1

How to create pandas DataFrame from nested Json with list?

Expected output will have 9 columns in DataFrame (data is retrieved from server ), have tried pd.json_normalize() but it didn't work

'{\n
  "a": "1",\n
  "b": "2",\n
  "c": "3",\n
  "d": "4",\n
  "cd": [\n
    {\n 
      "i": "1",\n
      "ii": "2",\n
      "iii": "3",\n
      "iv": "4",\n
      "v": "5"
    }\n
  ]\n
}'
2
  • So the columns should be a, b, c, d, i, ii, iii, iv, and v? Commented Sep 7, 2020 at 20:34
  • Does this answer your question? Pandas read nested json Commented Sep 7, 2020 at 21:01

2 Answers 2

2

Here is a simple solution. You need to use json_normalize and set the record path as the 'cd' column.

import json
import pandas as pd


js = '{\n "a": "1",\n "b": "2",\n "c": "3",\n "d": "4",\n "cd": [{"i": "1",\n "ii": "2",\n "iii": "3",\n "iv": "4",\n "v": "5"}]}'
js = json.loads(js)

df = pd.json_normalize(js, record_path=['cd'], meta=['a', 'b', 'c', 'd'])
print(df)
# >    i ii iii iv  v  a  b  c  d
# > 0  1  2   3  4  5  1  2  3  4

If you need want a more in-depth explanation of the json_normalize function, I would suggest you to read this article.

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

Comments

0

See, if you find this method useful.

import pandas as pd
temp="""{\n
  "a": "1",\n
  "b": "2",\n
  "c": "3",\n
  "d": "4",\n
  "cd": [\n
    {\n 
      "i": "1",\n
      "ii": "2",\n
      "iii": "3",\n
      "iv": "4",\n
      "v": "5"
    }\n
  ]\n
}"""
temp=eval(temp.replace("\n",""))
df=pd.DataFrame()
i=0
for key,val in temp.items():
    if len(temp[key][0])>1:
        for k,v in temp[key][0].items():
            df.loc[i,k]=v
    else:
        df.loc[i,key]=val
        
print(df)

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.