How to normalize a list from a json file using python?

Question:

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:

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

Asked By: Aks3

||

Answers:

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
Answered By: mozway