Flatten JSON using Python

11 Jun 2023

Based on a Stack Overflow answer, but with some enhancements added by me. I used this to pre-process JSON data before further processing.

Assumes Python 3.

(Another approach would have been to use JSONPath - for example jsonpath-ng).

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import json

# https://stackoverflow.com/a/55834113/12567365
# with some enhancements by me

sep = '_' # flattened key separator

def flatten(input_dict, prefix = ''):
    output_dict = {}
    for key, value in input_dict.items():
        if isinstance(value, dict) and value:
            nested = flatten(value, prefix + key + sep)
            output_dict.update({key2: val2 for key2, val2 in nested.items()})
        elif isinstance(value, list) and value:
            for index, sublist in enumerate(value, start = 1):
                if isinstance(sublist, dict) and sublist:
                    nested = flatten(sublist, prefix + key + sep + str(index) + sep)
                    output_dict.update({key2: val2 for key2, val2 in nested.items()})
                else:
                    output_dict[prefix + key + sep + str(index)] = value[index - 1]
        else:
            output_dict[prefix + key] = value
    return output_dict

json_string = '''
{
  "name": "Abel \\"Baker\\" Charlie",
  "score": 30,
  "color": "mauve",
  "level_2": {
    "active": true,
    "bar": [
      4,
      {
        "baz": {
          "bat": 5
        }
      },
      null
    ]
  }
}
'''

output = flatten(json.loads(json_string))
for key, value in output.items():
    print(key, "->", value)

Output:

1
2
3
4
5
6
7
name -> Abel "Baker" Charlie
score -> 30
color -> mauve
level_2_active -> True
level_2_bar_1 -> 4
level_2_bar_2_baz_bat -> 5
level_2_bar_3 -> None