I'm facing a problem here, because I must receive a json which contains a string with the path of some data in another json.
the Json which contains some data
json1 = { "Items": [{ "building": "buildingA", "y": [1, 2, 3], "yl": "A,B,C", "xl": "Data", "x": [1, 2, 3] }, { "y": [4, 5, 6], "x": [1, 2, 3], "predio": "BuildingB", "yl": "D,E,F", "xl": "Data" }] }and the json that contains the path of the desired value:
json2 = { "y": "y//1", }
I tried to make this code to solve the problem:
def size2(a,i):
x=a.split('//')
y=len(x)
if y ==1:
return i[x[0]]
elif y==2:
return i[x[0]][x[1]]
elif y==3:
return i[x[0]][x[1]][x[2]]
y=json2['y']
for i in json1['Items']:
print(i['y'][1]) #this way works
print(size2(y,i)) #this way fails
The error message is:
TypeERROR: list indices must be integers, not str
Does anyone know how to solve this problem?
int(x[1]).