Skip to content Skip to sidebar Skip to footer

Accessing Json Array In Python Without Referring To Its Name

I am new to python and I would like to understand how to access an array in a json object without referring to its name. The given json object has the below structure import json

Solution 1:

input_json = {
    "records": [
        {
            "values": {
                "col1": "1"
            },
            "no": 1,
        },
        {
            "values": {
                "col1": "2"
            },
            "no": 2,
        }
    ],
    "number_of_records": 2
}

for anything in input_json:
    if isinstance(input_json[anything], list):
        for values in input_json[anything]:
            print(values['values']['col1'])

You can also further nest the for loop if you don't know the 'values' and 'col1' names.


Post a Comment for "Accessing Json Array In Python Without Referring To Its Name"