0

I'm trying to print a list of COVID cases for each country using https://covid.ourworldindata.org/data/owid-covid-data.json

Here is my code:

import json

f = open('covidData.json')

data = json.load(f)

new_string = json.dumps(data, indent =2)

for i in new_string['AFG']:
        print(i)
f.close()

this only shows the data in "AFG" such as "continent", "location", etc. But I want to know how I can print "data" -> "new_cases"

I'm just learning python, but is there a syntax similar to ['AFG'.'data'.'new_cases'] that I can use to print the cases?

Extending this to the whole JSON file, can I upgrade my code so that I do not have to specify each country ID, rather use a general ['country'.'data'.'new_cases'] format to read the data?

Please let me know if anyone has any suggestions

C.Nivs
  • 12,353
  • 2
  • 19
  • 44
  • The data is in `data`. Work with that. Converting it straight back to JSON is pointless. – OrangeDog Feb 10 '21 at 18:36
  • Does this answer your question? [How to parse data in JSON format?](https://stackoverflow.com/questions/7771011/how-to-parse-data-in-json-format) – OrangeDog Feb 10 '21 at 18:38

2 Answers2

0

this only shows the data in "AFG"

As shown, your code should return an error.

new_string['AFG'] doesn't work because you cannot index a string by another string. In other words, json.dumps is not needed, data is a dictionary that you can index by a key, and you would have to iterate them one-by-one

e.g

for country_code, info in data.items():
  for country_info in info['data']:
    print('{}\t{}\t{}'.format(country_info['date'], info['location'], country_info['new_cases']))

is there a syntax similar to ['AFG'.'data'.'new_cases'] that I can use

Seems you want to use JSONPath, but I think the above solution is readable enough


Depending on your exact needs, using the CSV dataset in pandas would be easier to filter on and iterate over

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

It isn't clear exactly what you want to do - the data spans a number of days.

This code will show you a count of all the new cases for each country in the time period the data covers.

import json

with open('owid-covid-data.json') as data_file:
  covid_data = json.load(data_file)

for country in covid_data.keys():
  country_data = covid_data[country]
  location = country_data['location']
  cases_data = country_data['data']
  total_new_cases = 0
  for data in cases_data:
    total_new_cases += data.get('new_cases',0)

  print(f'{location} - {total_new_cases}')

This will list the new cases for each country by date.

import json

with open('owid-covid-data.json') as data_file:
  covid_data = json.load(data_file)

for country in covid_data.keys():
  country_data = covid_data[country]
  location = country_data['location']
  cases_data = country_data['data']
  total_new_cases = 0

  for data in cases_data:
    new_cases = data.get('new_cases',0)
    report_date = data['date']
    print(f'{location} - {report_date} - {new_cases}')
norie
  • 9,609
  • 2
  • 11
  • 18