Home > Mobile >  Parsing JSON request from COVID exposure website - Unable to print desired result
Parsing JSON request from COVID exposure website - Unable to print desired result

Time:09-24

I would like to create my own map which shows exposure sites. I'm sending a json request to an offical site and receiving an object that looks like this.

Output

Text output

{"help": "https://discover.data.vic.gov.au/api/3/action/help_show?name=datastore_search", "success": true, "result": {"include_total": true, "resource_id": "afb52611-6061-4a2b-9110-74c920bede77", "fields": [{"type": "int", "id": "_id"}, {"type": "text", "id": "Suburb"}, {"type": "text", "id": "Site_title"}, {"type": "text", "id": "Site_streetaddress"}, {"type": "text", "id": "Site_state"}, {"type": "text", "id": "Site_postcode"}, {"type": "text", "id": "Exposure_date_dtm"}, {"type": "text", "id": "Exposure_date"}, {"type": "text", "id": "Exposure_time"}, {"type": "text", "id": "Notes"}, {"type": "text", "id": "Added_date_dtm"}, {"type": "text", "id": "Added_date"}, {"type": "text", "id": "Added_time"}, {"type": "text", "id": "Advice_title"}, {"type": "text", "id": "Advice_instruction"}, {"type": "text", "id": "Exposure_time_start_24"}, {"type": "text", "id": "Exposure_time_end_24"}, {"type": "text", "id": "dhid"}], "records_format": "objects", "records": [{"_id":1,"Suburb":"Wodonga","Site_title":"Woolworths - White Box Rise Shopping Centre","Site_streetaddress":"Corner Victoria Cross Parade & Kelliher Avenue","Site_state":"VIC","Site_postcode":"3690","Exposure_date_dtm":"2021-09-19","Exposure_date":"19/09/2021","Exposure_time":"11:29am - 11:45am","Notes":"Case attended venue","Added_date_dtm":"2021-09-24","Added_date":"24/09/2021","Added_time":"15:08:09","Advice_title":"Tier 2 - Get tested urgently and isolate until you have a negative result","Advice_instruction":"Anyone who has visited this location during these times should urgently get tested, then isolate until confirmation of a negative result. Continue to monitor for symptoms, get tested again if symptoms appear.","Exposure_time_start_24":"11:29:00","Exposure_time_end_24":"11:45:00","dhid":"E8H3"},{"_id":2,"Suburb":"Berwick","Site_title":"Casey Superclinic","Site_streetaddress":"50 Kangan Drive\t","Site_state":"VIC","Site_postcode":"3806","Exposure_date_dtm":"2021-09-19","Exposure_date":"19/09/2021","Exposure_time":"11:00am - 11:45am","Notes":"Case attended venue","Added_date_dtm":"2021-09-24","Added_date":"24/09/2021","Added_time":"13:11:00","Advice_title":"Tier 2 - Get tested urgently and isolate until you have a negative result","Advice_instruction":"Anyone who has visited this location during these times should urgently get tested, then isolate until confirmation of a negative result. Continue to monitor for symptoms, get tested again if symptoms appear.","Exposure_time_start_24":"11:00:00","Exposure_time_end_24":"11:45:00","dhid":"C5E3"},{"_id":3,"Suburb":"Corio","Site_title":"Kmart - Corio Village Shopping Centre","Site_streetaddress":"Corner Bacchua Marsh & Purnell Road","Site_state":"VIC","Site_postcode":"3214","Exposure_date_dtm":"2021-09-19","Exposure_date":"19/09/2021","Exposure_time":"7:00pm - 7:55pm","Notes":"Case attended venue","Added_date_dtm":"2021-09-24","Added_date":"24/09/2021","Added_time":"11:35:00","Advice_title":"Tier 2 - Get tested urgently and isolate until you have a negative result","Advice_instruction":"Anyone who has visited this location during these times should urgently get tested, then isolate until confirmation of a negative result. Continue to monitor for symptoms, get tested again if symptoms appear.","Exposure_time_start_24":"19:00:00","Exposure_time_end_24":"19:55:00","dhid":"C2B4"},{"_id":4,"Suburb":"Bell Park","Site_title":"The Cheesecake Shop Geelong North","Site_streetaddress":"135 Separation Street","Site_state":"VIC","Site_postcode":"3125","Exposure_date_dtm":"2021-09-18","Exposure_date":"18/09/2021","Exposure_time":"2:15pm - 3:15pm","Notes":"Case attended venue","Added_date_dtm":"2021-09-24","Added_date":"24/09/2021","Added_time":"11:35:00","Advice_title":"Tier 2 - Get tested urgently and isolate until you have a negative result","Advice_instruction":"Anyone who has visited this location during these times should urgently get tested, then isolate until confirmation of a negative result. Continue to monitor for symptoms, get tested again if symptoms appear.","Exposure_time_start_24":"14:15:00","Exposure_time_end_24":"15:15:00","dhid":"C4C8"},{"_id":5,"Suburb":"Corio","Site_title":"Watan Supermarket and Halal Butcher","Site_streetaddress":"83C Purnell Road","Site_state":"VIC","Site_postcode":"3214","Exposure_date_dtm":"2021-09-19","Exposure_date":"19/09/2021","Exposure_time":"2:30pm - 3:30pm","Notes":"Case attended venue","Added_date_dtm":"2021-09-24","Added_date":"24/09/2021","Added_time":"11:35:00","Advice_title":"Tier 2 - Get tested urgently and isolate until you have a negative result","Advice_instruction":"Anyone who has visited this location during these times should urgently get tested, then isolate until confirmation of a negative result. Continue to monitor for symptoms, get tested again if symptoms appear.","Exposure_time_start_24":"14:30:00","Exposure_time_end_24":"15:30:00","dhid":"E9Z7"}], "limit": 5, "_links": {"start": "/api/3/action/datastore_search?limit=5&resource_id=afb52611-6061-4a2b-9110-74c920bede77", "next": "/api/3/action/datastore_search?offset=5&limit=5&resource_id=afb52611-6061-4a2b-9110-74c920bede77"}, "total": 533}}

What I would like to do is get a list with 'Site_streetaddress' however I haven't been succesful.

This is my code:

import urllib.request
import json
url = 'https://discover.data.vic.gov.au/api/3/action/datastore_search?resource_id=afb52611-6061-4a2b-9110-74c920bede77&limit=5'  
response = urllib.request.urlopen(url).read()
jsonResponse = json.loads(response.decode('utf-8'))
print(jsonResponse)

I tried the example here but I haven't got anything out of it. https://www.w3schools.com/python/python_json.asp

CodePudding user response:

Note that at top level there are only 3 keys.

import urllib.request
import json
url = 'https://discover.data.vic.gov.au/api/3/action/datastore_search?resource_id=afb52611-6061-4a2b-9110-74c920bede77&limit=5'  
response = urllib.request.urlopen(url).read()
jsonResponse = json.loads(response.decode('utf-8'))
print([item.get('Site_streetaddress') for item in jsonResponse['result']['records']])

output

['Corner Victoria Cross Parade & Kelliher Avenue', '50 Kangan Drive\t', 'Corner Bacchua Marsh & Purnell Road', '135 Separation Street', '83C Purnell Road']

As a side note I would recommend to use requests. After you install the package

import requests

url = 'https://discover.data.vic.gov.au/api/3/action/datastore_search?resource_id=afb52611-6061-4a2b-9110-74c920bede77&limit=5'  
response = requests.get(url)
data = response.json()
print([item.get('Site_streetaddress') for item in data['result']['records']])

CodePudding user response:

import requests


URL = 'https://discover.data.vic.gov.au/api/3/action/datastore_search?resource_id=afb52611-6061-4a2b-9110-74c920bede77&limit=5'

with requests.Session() as session:
    r = session.get(URL)
    r.raise_for_status()
    for _r in r.json()['result']['records']:
        print(_r['Site_streetaddress'])

CodePudding user response:

You need to travers through your jsonResponse as below:

import urllib.request
import json
url = 'https://discover.data.vic.gov.au/api/3/action/datastore_search?resource_id=afb52611-6061-4a2b-9110-74c920bede77&limit=5'
response = urllib.request.urlopen(url).read()
jsonResponse = json.loads(response.decode('utf-8'))
for data in jsonResponse['result']['records']:
    print ('Site_streetaddress: ' data['Site_streetaddress'])

CodePudding user response:

Great answers above. When extracting data from json, sometimes you need to 'peel' its layers. My process was:

> jsonResponse.keys()
dict_keys(['help', 'success', 'result'])
> jsonResponse['result'].keys()
dict_keys(['include_total', 'resource_id', 'fields', 'records_format', 'records', 'limit', '_links', 'total'])
# and so on ... Finally, I suggest
for i in jsonResponse['result']['records']:
    print(i["Site_streetaddress"])

And so on. I suggest: See https://www.kite.com/python/answers/how-to-extract-a-value-from-json-in-python

  • Related