pythonpandasjson-normalize

How to Normalize JSON data into a pandas dataframe with json_normalize


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

data={
    "customers": [
        {
            "a": 'true',
            "addresses": [{"city": "Park"}],
            "c": { "address1": "200"},
            "d": "mail@gmail.com",
            "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."


Solution

  • Your first key is "customers":

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