Parsing nested json in python
Question:
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"
]
}
]
}
}
}
Answers:
Just keep digging
Item1_url= data["items"]["Item1"]["subscriptions"][0]['url']
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"
]
}
]
}
}
}
Just keep digging
Item1_url= data["items"]["Item1"]["subscriptions"][0]['url']