Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading