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 a list from a json file using python?

I have a sample file like this

sample.json

{
   "school":[
      {
         "testId":123,
         "testName":"test1",
         "Teachers":[
            {
               "Tid":111,
               "Tname":"aaa"
            },
            {
               "Tid":222,
               "Tname":"bbb"
            },
            {
               "Tid":333,
               "Tname":"ccc"
            },
            {
               "Tid":444,
               "Tname":"ddd"
            }
         ],
         "location":"India"
      }
   ]
}

i need to normalize the Teachers list from this file, my code:

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

import json
import pandas as pd

with open('sample.json', 'r') as f:
    data = json.load(f)

df = pd.json_normalize(data, record_path='school', max_level=1)
df.to_csv("out.csv",index=False)

output:
enter image description here

I need to get an output like this:

enter image description here

is it possible to get this output without passing the keywords "Tid" and "Tname"?

>Solution :

You were almost there, use a list in record_path:

pd.json_normalize(data, record_path=['school', 'Teachers'])

Or subset the dictionary:

pd.json_normalize(data['school'], record_path='Teachers', max_level=1)

Output:

   Tid Tname
0  111   aaa
1  222   bbb
2  333   ccc
3  444   ddd

With helper columns:

pd.json_normalize(data['school'], record_path='Teachers',
                  meta=['testId', 'testName', 'location'], max_level=1)

Output:

   Tid Tname testId testName location
0  111   aaa    123    test1    India
1  222   bbb    123    test1    India
2  333   ccc    123    test1    India
3  444   ddd    123    test1    India
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