I have a JSON object like this below. Actually huge JSON file, simplified to ask this question.
"header": {
"headerAttributes": {
"pageCount": 5,
"totalCount": 5
}
},
"payload": [
{
"firstKey": [
{
"nestedKey_1": "1",
"nestedKey_2": "2"
},
{
"nestedKey_3": "3",
"nestedKey_4": "4"
}
]
}
]
Now I wrote below python code to access values inside [].
Total_Orders_Count = json_response ['header']['headerAttributes']['totalCount']
for i in range(0,Total_Orders_Count):
dict = json_response ['payload'][i]
inner_dict = dict ['firstKey'] [0]
print inner_dict ['nestedKey_1'] + '(' + inner_dict['nestedKey_2'] + ')'
The above code works fine. However, the usage of dict ['firstKey'] [0] doesn't work for values more than 1.
Two Questions. 1. Is there a better way to access Key values inside []. 2. Can we find length of number of values inside []. For this list, length of values under "firstKey" is 2.
Any help is appreciated.
dictlen(json_response['payload'][i]['firstKey'])