Skip to content Skip to sidebar Skip to footer

Iterate Over Json Object Using Python

I have a JSON object and was wondering how I can iterate over the object to pull values for 'id'. { 'totalSize': 5, 'done': true, 'records': [ { 'attributes': {

Solution 1:

You can iterate through the records key as a list and then access the Id key of each sub-dict:

for i in s["records"]:
    print(i['Id'])

Solution 2:

s = """{ "totalSize": 5, 
        "done": true, "records": [ 
            { "attributes": { 
                "type": "EventLogFile", 
                "url": "/services/data/v38.0/sobjects/EventLogFile/0AT1U000003kk7dWAA" }, 
                "Id": "0AT1U000003kk7dWAA" } 
        ] 
    }"""
s = json.loads(s)

[r['Id'] for r in s['records']]

['0AT1U000003kk7dWAA']

Post a Comment for "Iterate Over Json Object Using Python"