Home > Blockchain >  Flatten a nested JSON?
Flatten a nested JSON?

Time:05-14

I am trying to flatten the following JSON and flatten it hierarchically: https://justpaste.it/6e60p

I am using pandas json_normalize function to do this but I am bit stuck.

pd.json_normalize(test_json['result'])

Gives me 2 columns with nested dicts. I am trying to use record_path=['userDetails'] but then it opens only the user part.

Please advise what is the best practice to flatten the JSON hierarchically?

CodePudding user response:

flatten_json is a library now, so you can do this. It'll give you 160 columns

from flatten_json import flatten
dic_flattened = (flatten(d, '.') for d in test_json['result'])
df = pd.DataFrame(dic_flattened)

df.shape
(5, 160)
  • Related