How to Normalize JSON data into a pandas dataframe with json_normalize

89 Views Asked by At

I'm trying to transform this complex object into a pandas dataframe

data={
    "customers": [
        {
            "a": 'true',
            "addresses": [{"city": "Park"}],
            "c": { "address1": "200"},
            "d": "[email protected]",
            "e": {"f": "sub"},
            "h": 100,
           
        }
    ]
}

I've tried several ways but none of them work.

df = pd.json_normalize(data,record_path='addresses',meta=['h'] ,record_prefix='adr'
                               ,errors='ignore')

I always get the same error

KeyError: "Key 'addresses' not found. If specifying a record_path, all elements of data should have the path."

1

There are 1 best solutions below

0
Jason Baker On BEST ANSWER

Your first key is "customers":

df = pd.json_normalize(
    data=data.get("customers"),
    record_path="addresses",
    meta="h",
    record_prefix="adr"
)

print(df)