Home > database >  Accessing unique json array
Accessing unique json array

Time:02-22

I'm aware the code won't work because I can't work out how to access each array, i.e. Person-Ryan and Personal-Luke. There will be multiple and each will be unique and won't know what they be called beforehand.

I would like to print the name on each one in a loop.

JSON:

"entities": {
    "Person-Ryan": {
        "name":"Ryan"
    },
    "Person-Luke": {
        "name": "Luke"
   }
}

Code:

response = requests.request("GET", url, headers=headers, data=payload)
res_json = response.json()

for inner_dict in res_json:
    Test = inner_dict['name']
    print (Test)

CodePudding user response:

I think what you need is just calling items function on the dictionary that you get from response.json():

for key, value in res_json["entities"].items():
  print(value["name"])

Output

Ryan
Luke
  • Related