0

Let's say I have some JSON as below

response = {
        "totalrecords": 2,
        "data": [
            {
                "stateCd": "U.K",
                "stateName": "uttarakhand",
                "details": {
                    "id": [
                        "2312-k",
                        "2312-k"
                    ],
                    "date": [
                        "10-OCT-2019",
                        "11-OCT-2019"
                    ],
                    "icp": [
                        2233,
                        6443
                    ],
                    "icpr": [
                        3.434,
                        23.232
                    ]
                }
            },
            {
                "stateCd": "U.P",
                "stateName": "uttar pradesh",
                "details": {
                    "id": [
                        "2712-k",
                        "5412-k"
                    ],
                    "date": [
                        "10-OCT-2019",
                        "11-OCT-2019"
                    ],
                    "icp": [
                        2233,
                        6443
                    ],
                    "icpr": [
                        32.434,
                        31.232
                    ]
                }
            }
        ]
    }

I wanted to convert it into data frame as below

enter image description here

but while trying to convert it into dataframe using pandas.json_normalize() I am unable to reach my desired output

what I have tried:

data_trunc=response['data'] # to extract data from response
pd.json_normalize(data_trunc)

enter image description here

pd.json_normalize(data_trunc,record_path=['details','id'],meta=['stateCd','stateName'])

enter image description here

but this don't include date, icp, icpr columns

so I have tried different permutation and combination

    pd.json_normalize(data_trunc,record_path=[['details','id'],['date']],meta=['stateCd','stateName'])

pd.json_normalize(data_trunc,record_path=[['details','id'],['details'.'date']],meta=['stateCd','stateName'])

but landed up to same error TypeError: unhashable type: 'list'

1
  • Your response = {...} is not actually JSON, it is a Python dict, which looks like JSON when formatted like this. (although that's okay here, as pandas.json_normalize() does take dict input) Commented Jun 10, 2021 at 14:12

1 Answer 1

2

You need to explode.

pd.json_normalize(data_trunc).apply(pd.Series.explode)

stateCd stateName details.id details.date details.icp details.icpr
0 U.K uttarakhand 2312-k 10-OCT-2019 2233 3.434
0 U.K uttarakhand 2312-k 11-OCT-2019 6443 23.232
1 U.P uttar pradesh 2712-k 10-OCT-2019 2233 32.434
1 U.P uttar pradesh 5412-k 11-OCT-2019 6443 31.232
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.