Skip to content Skip to sidebar Skip to footer

Parsing Nested Json Payload Python

I am trying to get only value A from from this nested json payload. My function: import requests import json def payloaded(): from urllib.request import urlopen with urlopen('www.e

Solution 1:

Since the id value is a list (even though it just contains a single value), you'll need to go inside it with a list indexer. Since lists in Python are zero-indexed (they start from zero) you'll use [0] to extract the first element:

data["bod"]["id"][0]["value"]

Solution 2:

This works:

def payloaded():
from urllib.request import urlopen
with urlopen("www.example.com/payload.json") as r:
    data = json.loads(r.read().decode(r.headers.get_content_charset("utf-8")))
text = (data["bod"]["id"][0]["value"])
print(text)

Post a Comment for "Parsing Nested Json Payload Python"