Home > Mobile >  How do I get values from the json dictionary?
How do I get values from the json dictionary?

Time:12-25

I want to get the values of live data from the air quality london api however I don't know how to get the values from the strange dictionary like thing that is outputted.

Here is my code:

def get_live_data_from_api(site_code='MY1',species_code='NO',start_date=None,end_date=None):

start_date = datetime.date.today() if start_date is None else start_date
end_date = start_date   datetime.timedelta(days=1) if end_date is None else end_date


endpoint = "https://api.erg.ic.ac.uk/AirQuality/Data/SiteSpecies/SiteCode={site_code}/SpeciesCode={species_code}/StartDate={start_date}/EndDate={end_date}/Json"

url = endpoint.format(
    site_code = site_code,
    species_code = species_code,
    start_date = start_date,
    end_date = end_date
)

res = (requests.get(url)).json()
print(res)    

here is the output:

{'RawAQData': {'@SiteCode': 'MY1', '@SpeciesCode': 'NO', 'Data': [{'@MeasurementDateGMT': '2022-12-24 00:00:00', '@Value': '13.5'}, {'@MeasurementDateGMT': '2022-12-24 01:00:00', '@Value': '9.8'}, {'@MeasurementDateGMT': '2022-12-24 02:00:00', '@Value': '8.7'}, {'@MeasurementDateGMT': '2022-12-24 03:00:00', '@Value': '7.9'}, {'@MeasurementDateGMT': '2022-12-24 04:00:00', '@Value': '8.1'}, {'@MeasurementDateGMT': '2022-12-24 05:00:00', '@Value': '10.1'}, {'@MeasurementDateGMT': '2022-12-24 06:00:00', '@Value': '19.7'}, {'@MeasurementDateGMT': '2022-12-24 07:00:00', '@Value': '22.1'}, {'@MeasurementDateGMT': '2022-12-24 08:00:00', '@Value': '24'}, {'@MeasurementDateGMT': '2022-12-24 09:00:00', '@Value': '34.2'}, {'@MeasurementDateGMT': '2022-12-24 10:00:00', '@Value': '32.2'}, {'@MeasurementDateGMT': '2022-12-24 11:00:00', '@Value': '42.2'}, {'@MeasurementDateGMT': '2022-12-24 12:00:00', '@Value': '41.8'}, {'@MeasurementDateGMT': '2022-12-24 13:00:00', '@Value': '33.2'}, {'@MeasurementDateGMT': '2022-12-24 14:00:00', '@Value': '49'}, {'@MeasurementDateGMT': '2022-12-24 15:00:00', '@Value': '45.2'}, {'@MeasurementDateGMT': '2022-12-24 16:00:00', '@Value': '53'}, {'@MeasurementDateGMT': '2022-12-24 17:00:00', '@Value': '44.4'}, {'@MeasurementDateGMT': '2022-12-24 18:00:00', '@Value': '38.7'}, {'@MeasurementDateGMT': '2022-12-24 19:00:00', '@Value': '17.6'}, {'@MeasurementDateGMT': '2022-12-24 20:00:00', '@Value': '14.4'}, {'@MeasurementDateGMT': '2022-12-24 21:00:00', '@Value': '12.2'}, {'@MeasurementDateGMT': '2022-12-24 22:00:00', '@Value': ''}]}}

I would like to get all of the "@Values" values from this dictionary, how would I go about doing it?

CodePudding user response:

I got this demo code from Get API call. API help in here

import requests
import datetime

def get_live_data_from_api(site_code='MY1', species_code='NO',start_date=None,end_date=None):
    start_date = datetime.date.today() if start_date is None else start_date
    end_date = start_date   datetime.timedelta(days=1) if end_date is None else end_date

    url = 'https://api.erg.ic.ac.uk/AirQuality/Data/SiteSpecies/SiteCode='   site_code   \
        '/SpeciesCode='   species_code   \
        '/StartDate='   start_date   \
        '/EndDate='  end_date   '/Json'
    r = requests.get(url,
        headers={'Accept': 'application/json'})
    data = r.json()
    return data['RawAQData']['Data']

results = get_live_data_from_api('MY1','NO','2022-12-11', '2022-12-12')
for data in results:
    print(data['@MeasurementDateGMT']   " -> "   data['@Value'])

Result

$ python get-data.py
2022-12-11 00:00:00 -> 113.8
2022-12-11 01:00:00 -> 150.7
2022-12-11 02:00:00 -> 177.8
2022-12-11 03:00:00 -> 201.9
2022-12-11 04:00:00 -> 161.8
2022-12-11 05:00:00 -> 130.1
2022-12-11 06:00:00 -> 92.2
2022-12-11 07:00:00 -> 90.7
2022-12-11 08:00:00 -> 69.2
2022-12-11 09:00:00 -> 52.3
2022-12-11 10:00:00 -> 46.8
2022-12-11 11:00:00 -> 41.2
2022-12-11 12:00:00 -> 42.5
2022-12-11 13:00:00 -> 34
2022-12-11 14:00:00 -> 35.5
2022-12-11 15:00:00 -> 39.9
2022-12-11 16:00:00 -> 50.8
2022-12-11 17:00:00 -> 31.5
2022-12-11 18:00:00 -> 21.9
2022-12-11 19:00:00 -> 22.4
2022-12-11 20:00:00 -> 20.7
2022-12-11 21:00:00 -> 17.9
2022-12-11 22:00:00 -> 11.7
2022-12-11 23:00:00 -> 9

CodePudding user response:

for o in res['RawAQData']['Data']:
    print(o['@Value'])

# or to create a list of all the values:
values = [o['@Value'] for o in d['RawAQData']['Data']]
  • Related