-2

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?

  • https://www.w3schools.com/python/python_json.asp – KaraX_X Dec 24 '22 at 22:54
  • 1
    Your indentation is clearly wrong, but what’s less clear is how this ought to be indented. Don’t make us guess; please [edit] to fix this. (On the desktop version of this site, paste your program, select it, and type ctrl-K to properly format it as code.) – tripleee Dec 24 '22 at 23:09
  • Does this answer your question? [How to use a Python dictionary?](https://stackoverflow.com/questions/45072283/how-to-use-a-python-dictionary) – JonSG Dec 24 '22 at 23:45
  • Can you provide {start_date} example. I try this but it did not works. https://api.erg.ic.ac.uk/AirQuality/Data/SiteSpecies/SiteCode=MY1/SpeciesCode=NO/StartDate=2022-12-24/EndDate=2022-12-24/Json – Bench Vue Dec 25 '22 at 00:53
  • Got it this format works `https://api.erg.ic.ac.uk/AirQuality/Data/SiteSpecies/SiteCode=MY1/SpeciesCode=NO/StartDate=2019-12-11/EndDate=2019-12-12/Json` – Bench Vue Dec 25 '22 at 00:55

1 Answers1

1

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
Bench Vue
  • 5,257
  • 2
  • 10
  • 14