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 do you extract multiple values from json in python

I have a json file like this:

print(resp)
dataList = []
{
  "totalCount": 9812,
  "pageSize": 50,
  "nextPageKey": "12345",
  "problems": [
    {
      "problemId": "1234",
      "displayId": "test1",
      "title": "host memory saturation",
      "impactLevel": "INFRASTRUCTURE",
      "severityLevel": "RESOURCE_CONTENTION",
      "status": "OPEN"
     }
     ]
}

I need to extract "title", "impactLevel" and "severityLevel" and create a data frame out this:

I have tried this:

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

dataList.append([resp['problems'][0]['title']['impactLevel']['severityLevel']])
hyperEntitydf = pd.DataFrame(dataList, columns=['Alert','Type','Severity'])
hyperEntitydf=hyperEntitydf.drop_duplicates()
print(hyperEntitydf.head())

On this line:

 dataList.append([resp['problems'][0]['title']['impactLevel']['severityLevel']])

I am getting this error:

TypeError: string indices must be integers

Is it possible to extract multiple fields with one call:

dataList.append([resp['problems'][0]['title']['impactLevel']['severityLevel']])

>Solution :

No, it’s not possible. For this to work, the json would need to be nested such that impactLevel were within title, and severityLevel were within impactLevel.

I’d suggest

x = resp['problems'][0]
dataList.append([x['title'], x['impactLevel', x['severityLevel'])

Unless, you want all the fields of the json, in which case you can do:

dataList.append(list(resp['problems'][0].values()))
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