I have a json file from an API response that broadly follows the following schema
import pandas as pd
j =[
{
"orders": [
{
"orderId": 0,
"items": [
{ "item_1": "x", "item_price": 5.99 },
{ "item_1": "y", "item_price": 15.99 }
]
}
]
}
]
What I'm trying to do is to normalize the json but retain the orderId from the top level so I can re-join the datasets further down a data pipeline.
I thought this could be done with one of the keyword arguments in pd.json_normalize
df_orders = pd.json_normalize(j,record_path=['orders'])
print(df_orders)
orderId items
0 0 [{'item_1': 'x', 'item_price': 5.99}, {'item_1...
df_items = pd.json_normalize(j,record_path=['orders','items'])
print(df_items)
item_1 item_price
0 x 5.99
1 y 15.99
What I would like is
item_1 item_price orders.orderId
0 x 5.99 0
1 y 15.99 0
--
I've gone through the documentation here
and attempted to use
pd.json_normalize(j,record_path=['orders','items'], meta=['orders'])
which just appends the entire json object to each record.
item_1 item_price orders
0 x 5.99 {'orderId': 0, 'items': [{'item_1': 'x', 'item...
1 y 15.99 {'orderId': 0, 'items': [{'item_1': 'x', 'item...
any tips using pd.json_normalize?
I have no issue in parsing the json out manually and doing this in a 2 step process but want to avoid further depencies in my pipeline to keep things dynamic if i can help it.