I have a json response like this
order_response = {
"orders": [
{
"id": '1',
"email": "[email protected]",
"location_id": 9,
"line_items": [
{
"id": 5,
"product_id": 6,
}, {
"id": 7,
"product_id": 8,
}
]
}, {
"id": '2',
"email": "[email protected]",
"location_id": 10,
"line_items": {
"id": 3,
"product_id": 4,
}
},
]
}
And I wanted the output like this
id email location_id line_items_id line_items_product_id
1 [email protected] 9 5 6
1 [email protected] 9 7 8
1 [email protected] 10 3 4
I want to split the rows as per the number of objects in the line_items.
So my approach is to use the json_normalize feature of Pandas
I am able to spilt if I specify the column names in the code as shown below.
pd.io.json.json_normalize(report_json, ['line_items'], ['id', 'email'], record_prefix='line_items_')
but there may be other columns apart from id, email. I want this to be dynamic i.e. it should be able to do with any number of objects provided without explicitly defining
Any help in this regard is highly appreciated.