I want to count the number of objects in an array. The first one is easy len(d['line_of_businesses'])
but it isn't obvious how from the example below I would count how many objects are in the array called commercial_situations
which is 3 deep. Thanks, Matt
data = {
"line_of_businesses": [
{
"asset_groups": {
"commercial_situations": [
{
"asset_id": "d222cecf-af58-465a-9506-e76807d7d9tf"
}
]
}
}
]
}
CodePudding user response:
The following code will do the job for you. For More Info Click Here
data = {
"line_of_businesses": [
{
"asset_groups": {
"commercial_situations": [
{
"asset_id": "d222cecf-af58-465a-9506-e76807d7d9tf"
},
{
"asset_id": "1222cecf-af58-465a-9506-e76807d7d9tf"
}
]
}
}
]
}
print(len(data['line_of_businesses'][0]['asset_groups']['commercial_situations']))
How it works? First get the items in line_of_buisness
then this is an array so we need its first element so [0]
and inside this we need data of asset_groups
so ['asset_groups']
and then inside this we need commercial_situations
.