Complete noob in python here, I'm trying to retrieve some values from a JSON response and I'm facing a 'str' object has no attribute error I don't work around, it seems because what I retrieve changes types.
Here is the json stored in "response":
{
"root": {
"id": "683308188000",
"type1": {
"type1_1": {
"id": "683308185000",
"sub_info1": {
"sub_info1_1": {
"name": "thing"
}
}
}
},
"multiType_1": {
"multiType_1_1": [
{
"id": "24567876543456",
"name": {
"name2": {
"name": "grerggre"
}
}
},
{
"id": "234567654334",
"name": {
"name2": {
"name": "grerggre"
}
}
}
]
},
"multiType_2": {
"multiType_2_2": {
"id": "683308570000",
"name": {
"name2": {
"name": "grerggre"
}
}
}
}
}
}
Now here my code to retrieve all id :
result1 = []
result2 = []
for x in response.root.multiType1.multiType1_1:
result1.append(x.id)
for x in response.root.multiType2.multiType2_2:
result2.append(x.id)
result1 works, but for result2 I get a 'str' object has no attribute error. I can get the value in this specific case via response.root.multiType2.multiType2_2.id, but I don't know in advance I there will be one or multiple occurrences of each "multiType".
Is there a way to get around this ? Many thanks.