简体   繁体   中英

Accessing data in a JSON tree

I am trying to access JSON data on a website ( https://petition.parliament.uk/petitions/301397.json ) for visualisation purposes. I am trying to plot the "signatures_by_country" segment but just cannot seem to access this with the following code

info = json.loads (data)
print (info)
print (info["signatures_by_country"][0]["name"])

The second print errors out. Below is a sample of the data

{"links":{"self":"https://petition.parliament.uk/petitions/301397.json"},"data":{"type":"petition","id":301397,"attributes":{"action":"Implement UK lockdown for preventing spread of COVID19","background":"The UK needs to follow suit the containment procedures of countries that have been greatly affected by COVID19 such as Italy.\r\n\r\nThe UK should restrict unnecessary travel between towns and cities. Travel permitted should only be for work or emergencies. Public gatherings should also be discouraged.","additional_details":"It is time the government should prioritise the health of the public and should consider how vital it is to implement effective containment now.\r\n\r\nIt is better to spend money to contain the virus and treating the relatively low numbers who are ill now rather than wait for more casualties.","committee_note":"","state":"open","signature_count":190004,"created_at":"2020-03-11T00:16:42.594Z","updated_at":"2020-03-15T09:37:54.000Z","rejected_at":null,"opened_at":"2020-03-12T16:54:59.310Z","closed_at":null,"moderation_threshold_reached_at":"2020-03-11T13:44:24.000Z","response_threshold_reached_at":"2020-03-13T10:32:54.000Z","government_response_at":null,"debate_threshold_reached_at":"2020-03-14T10:57:34.000Z","scheduled_debate_date":null,"debate_outcome_at":null,"creator_name":"Beamari Alarva Suerte","rejection":null,"government_response":null,"debate":null,"departments":[{"acronym":"DHSC","name":"Department of Health and Social Care","url":"https://www.gov.uk/government/organisations/department-of-health-and-social-care"}],"signatures_by_country":[{"name":"Albania","code":"AL","signature_count":2},{"name":"Australia","code":"AU","signature_count":19},{"name":"Austria","code":"AT","signature_count":6},{"name":"Bahrain","code":"BH","signature_count":2},{"name":"Belarus","code":"BY","signature_count":1},{"name":"Belgium","code":"BE","signature_count":7},{"name":"Brazil","code":"BR","signature_count":4},{"name":"Bulgaria","code":"BG","signature_count":3},{"name":"Cambodia","code":"KH","signature_count":1},{"name":"Canada","code":"CA","signature_count":22},{"name":"Cayman Islands","code":"KY","signature_count":1},{"name":"Chile","code":"CL","signature_count":1},{"name":"China","code":"CN","signature_count":20},{"name":"Colombia","code":"CO","signature_count":1},{"name":"Cyprus","code":"CY","signature_count":12},{"name":"Czechia","code":"CZ","signature_count":7},{"name":"Denmark","code":"DK","signature_count":10},{"name":"Egypt","code":"EG","signature_count":2},{"name":"Estonia","code":"EE","signature_count":1},{"name":"Finland","code":"FI","signature_count":6},{"name":"France","code":"FR","signature_count":92},{"name":"Germany","code":"DE","signature_count":34},{"name":"Gibraltar","code":"GI","signature_count":6},{"name":"Greece","code":"GR","signature_count":16},{"name":"Guernsey","code":"GG","signature_count":7},{"name":"Hong Kong","code":"HK","signature_count":41},{"name":"Hungary","code":"HU","signature_count":4},{"name":"

Please try:

print(info["data"]["attributes"]["signatures_by_country"][0]["name"])

In order to better understand and modify your JSON data I suggest you to use a JSON Editor.
Once you visualize your data and you can interact with it in realtime the error you made will become obvious.

Here's one I like but there are countless options out there:
https://jsoneditoronline.org/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM