I have an object like this
ds_obj = {
"Labels" :
[
{
"Label" : "pop",
"People" : {
"Composer" : "John"
},
},
{
"Label" : "classical",
"People" : {
"Composer" : [ "Johann", "Jason" ]
},
},
{
"Label" : "classical-2",
"People" : {
"Composer" : [ "Jacob" ]
},
}
]
}
I need to iterate thru each of the labels and perform some actions on each of the composers, but first I need to find out if each label has a single composer or multiple. To do this I am running the following code to find out if the key Composer
has an array or a string. If it has an array, I will run a nested loop thru it and work with each composer. The code below however is calling out every value as an array. What is wrong with my code? Is there a more efficient way to do this?
for label in ds_obj ['Labels']:
if hasattr(label['People']['Composer'], "__len__"):
print('IS ARRAY')
else:
print('NOT Array')