How to Normalize JSON data into a pandas dataframe with json_normalize

Question:

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."

Asked By: Herojos

||

Answers:

Your first key is "customers":

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

print(df)
Answered By: Jason Baker
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.