How can I quickly flatten a JSON dictionary?

A very niche use case but I had a use case once for flattening a JSON object such that it went from this

  "source": {
    "service": "blah"

into this

  "source.service": "blah"

In this case, the latter was easy to parse dynamically because it means the file would always have a depth of 1 instead of a possibly infinite depth to recurse through.

Anyway, this was thankfully easier than I expected using a Python library called FlatDict

Here’s an example of how to use it:

import flatdict

d = flatdict.FlatDict(expect, delimiter='.')

How easy was that?

I haven’t checked if the FlatDict type is serialisable as JSON but if not, you can just convert back to a dictionary using dict(d).

A fuller example can be found in the form of this Github Gist.