Having difficulty trying to convert a nested JSON object to a dataframe in my desired format. Assumed this would be really simple, but nearly tearing my hair out!
Here is an example of my JSON structure.
{
"recipe1" : {
"abbie" : 2,
"ben" : 3,
"chris" : 1
},
"recipe2" : {
"abbie" : 1,
"ben" : 5,
"chris" : 5
}
}
After trying a few different options I decided to use the pandas library as it seems the easiest to use.
Result in this example is a query to my database fetching the JSON data which I managed to get working.
dataframe = json_normalize(result)
print(dataframe)
Gives me the following on a single line:
recipe1.abbie | recipe1.ben | recipe1.chris | recipe2.abbie | ..
2 | 3 | 1 | 1 | ..
Although ideally I would like the data frame to look like this:
| Recipe 1 | Recipe 2 |
Abbie | 2 | 1 |
Ben | 3 | 5 |
Chris | 1 | 5 |
After looking on this site and elsewhere I believe the Pivot function is what I need to use, but after trying all morning I'm no closer to the solution unfortunately.
thanks in advance for any help, would greatly appreciate it.