简体   繁体   中英

Parsing nested json in python

I'm trying to parse the following json to get the Item (Item1, Item2 ...) and URL fields out of the payload. I'm able to parse out the Item1, Item2 no problem with something like this but I'm at a loss of how to get the URL out of it as well

resp = requests.get(url)
data = json.loads(resp.text)
items = data["items"]
for item in items:
    print(item)

I've tried adding something like this just to get the subscriptions but receive KeyError: 0

subscriptions = data["items"][0]

Payload:

{
  "items": {
    "Item1": {
      "subscriptions": [
        {
          "url": "https://someurl.com",
          "name": "Customer1",
          "categories": [
            "Appointment"
          ]
        }
      ]
    },
    "Item2": {
      "subscriptions": [
        {
          "name": "Customer2",
          "url": "https://someotherurl.com",
          "authPass": "",
          "authUser": "",
          "categories": [
            "Appointment"
          ]
        }
      ]
    }
  }
}

只是继续挖

Item1_url= data["items"]["Item1"]["subscriptions"][0]['url']

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