I am following this tutorial https://towardsdatascience.com/how-to-convert-json-into-a-pandas-dataframe-100b2ae1e0d8 and build something like this.
The sample json data:
{
"product":{
"a":"apple",
"b":"banana"
},
"order":[{
"code":"123",
"qty":"1",
"food":"apple x10|banana x2",
"amount":"200"},
{
"code":"bundle"
"qty":"1"
}
]
}
There is my code:
df = pd.json_normalize(
data,
record_path =['order'],
meta=[
['product', 'a'],
['product', 'b']
]
)
df = df.fillna(0)
And this will output:
code,qty,food,amount,a,b
123,1,apple x10|banana x2,200,apple,banana
bundle,1,0,0,apple,banana
Because I need to fill zero in the missing elements columns, I let the json to be dataframe first. But when I finish this, I don't know how to get back the origin json format.
I need the output like sample json:
{
"product":{
"a":"apple",
"b":"banana"
},
"order":[{
"code":"123",
"qty":"1",
"food":"apple x10|banana x2",
"amount":"200"},
{
"code":"bundle"
"qty":"1",
"food":"0",
"amount":"0"
}
]
}
Any help would be appreciated