1

I want to print out some values from my json file and here is part of the code:

{
   "records" : [
      {
         "dateRep" : "02/05/2021",
         "day" : "02",
         "month" : "05",
         "year" : "2021",
         "cases" : 1655,
         "deaths" : 16,
         "countriesAndTerritories" : "Austria",
         "geoId" : "AT",
         "countryterritoryCode" : "AUT",
         "popData2020" : "8901064",
         "continentExp" : "Europe"
      },

How can i print dateRep ,cases, deaths or any value i want and put it in a variable to use it? I am using this code to load my json file:

f = open('DailyUpdatedReport.json',)
data = json.load(f)

print(data)

My other issue : My json file need to be up-to dated I don't know how to do it. Here is URL for the json file I am using
https://opendata.ecdc.europa.eu/covid19/nationalcasedeath_eueea_daily_ei/json/

martineau
  • 119,623
  • 25
  • 170
  • 301
Q8Bader
  • 101
  • 5

1 Answers1

1

You would do this just how you would get data from any other dictionary in python. I'll put an example below, and you should be able to generalize from there. In response to your second question, I don't understand what you mean by needing to update your data. Couldn't you just copy and paste the data from the link you posted into a json?

import json

f = open('DailyUpdatedReport.json',)
data = json.load(f)

#['records']: enter records entry in data
#[0]: go into the first record
#['dateRep']: read a value from that record (in this case dateRep)
dateRep = data['records'][0]['dateRep']
print(dateRep)
  • Thank. In the URL the data are updated day by day but for now i am just like you said i am copying the json file and paste it. I need to use theses data but through the URL ( i guss) to have it auto updated in my project. – Q8Bader May 03 '21 at 20:19
  • 1
    Oh, I didn't realize that was what you were asking for. Check out [this](https://stackoverflow.com/questions/12965203/how-to-get-json-from-webpage-into-python-script) question for how to do that. – Harry Albert May 04 '21 at 13:15