I'm trying to unnest a json file. The JSON has multiple lists of dictionaries inside a list of dictionaries. I'm trying to flatten everything in it and turn it into a dataframe. it looks something like this:
{
"Result": [
{
"OptionalColumns": {
"optionalColumnName": "Joe Blogs"
},
"fieldOne": "some string",
"fieldtwo": "some more string",
"fieldthree": "even more string",
"secondList": [
{
"secondListFieldOne": "value",
"secondListFieldTwo": 0,
"secondListFieldThree": true
},
{
"secondListFieldOne": "value",
"secondListFieldTwo": 0,
"secondListFieldThree": true
}
],
"anotherField": "string value",
"thirdList": [
{
"thirdListFieldOne": "string",
"thirdListFieldTwo": "string"
}
],
"someNumberValue": 1
},
{
"OptionalColumns": {
"optionalColumnName": "Joe Blogs"
},
"fieldOne": "some string",
"fieldtwo": "some more string",
"fieldthree": "even more string",
"secondList": [
{
"secondListFieldOne": "value",
"secondListFieldTwo": 0,
"secondListFieldThree": true
},
{
"secondListFieldOne": "value",
"secondListFieldTwo": 0,
"secondListFieldThree": true
}
],
"anotherField": "string value",
"thirdList": [
{
"thirdListFieldOne": "string",
"thirdListFieldTwo": "string"
}
],
"someNumberValue": 1
}
],
"Message": null,
"Errors": []
}
I'm using the below method to unnest. I know this works with one nest, or even two, however I can't for the life of me get it to work. HELP.
with open('data/my_file.json','r') as f:
json_data = json.loads(f.read())
df_unnested_list = pd.json_normalize(json_data, 'Result')
When trying to unnest a list of dicts:
pd.json_normalize(data, "field", ["fieldTwo", "nestFieldOne"])
It would be how you posted it before with another dict. Quite simple really.
pd.json_normalize(data, “thirdList”, [“OptionalColumns”, “optionalColumnName”],”fieldOne”, “fieldTwo”, “fieldThree”, [“secondList”, “secondListDictOneFieldOne”,”secondListDictTwoFieldOne”], “anotherField”, “someNumberValue”)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.