1

I would like to convert nested json to dataframe.

The result i want to get :

| securityFullTypeCode | securityTypeDesc |
| - - - - - - - - - - -| - - - - - - - - -|
| 0101                 |  Ordinary Share  |
| 0102                 |  Preferred  Share|

I tried json_normalize but unsuccessfully.

Json file looks like this :

{
    "securitiesTypes": {
        "result": [
            {
                "securityFullTypeCode": "0101",
                "securityTypeDesc": "Ordinary Share"
            },
            {
                "securityFullTypeCode": "0102",
                "securityTypeDesc": "Preferred  Share"
            },
            {
                "securityFullTypeCode": "0103",
                "securityTypeDesc": "Preferr & Cum  share"
            }
        ],
        "total": 215
    }
}

Thanks

2
  • 1
    try : pd.DataFrame(pd.read_json(file.json').loc['result','securitiesTypes']) Commented Feb 26, 2021 at 15:07
  • 1
    @anky nice solution. Commented Feb 26, 2021 at 15:15

2 Answers 2

1

A similar question was asked in: JSON to pandas DataFrame

The following seems to do the trick:

file = {
    "securitiesTypes": {
        "result": [
            {
                "securityFullTypeCode": "0101",
                "securityTypeDesc": "Ordinary Share"
            },
            {
                "securityFullTypeCode": "0102",
                "securityTypeDesc": "Preferred  Share"
            },
            {
                "securityFullTypeCode": "0103",
                "securityTypeDesc": "Preferr & Cum  share"
            }
        ],
        "total": 215
    }
}

df = pd.json_normalize(file['securitiesTypes']['result'])

What problems are you running into exactly?

Sign up to request clarification or add additional context in comments.

2 Comments

I got the following error: TypeError: string indices must be integers
I would check out the following in that case: stackoverflow.com/questions/6077675/…
1

Use json_normalize with record_path option:

import json

with open('file.json') as f:
    df = pd.json_normalize(json.load(f), 
                           record_path=['securitiesTypes','result']
                          )

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.