-1

I have the json code below and I have a list

i want to do a for loop or if statement which

if label in selected_size:
  fsize = id

selected_size[]

in selected size:

[7, 7.5, 4, 4.5]

in json:

removed
print(json_data)
for size in json_data:
    if ['label'] in select_size:
        fsize = ['id']
print(fsize)

i have no idea on how to do it.

Tony
  • 29
  • 6

1 Answers1

1

You need to access to list and later to dict, for example:

json_data = [{'id': '91', 'label': '10.5', 'price': '0', 'oldPrice': '0', 'products': ['81278']}, {'id': '150', 'label': '9.5', 'price': '0', 'oldPrice': '0', 'products': ['81276']}, {'id': '28', 'label': '4', 'price': '0', 'oldPrice': '0', 'products': ['81270']}, {'id': '29', 'label': '5', 'price': '0', 'oldPrice': '0', 'products': ['81271']}, {'id': '22', 'label': '8', 'price': '0', 'oldPrice': '0', 'products': ['81274']}, {'id': '23', 'label': '9', 'price': '0', 'oldPrice': '0', 'products': ['81275']}, {'id': '24', 'label': '10', 'price': '0', 'oldPrice': '0', 'products': ['81277']}, {'id': '25', 'label': '11', 'price': '0', 'oldPrice': '0', 'products': ['81279']}, {'id': '26', 'label': '12', 'price': '0', 'oldPrice': '0', 'products': ['81280']}]
fsize = []
select_size = [7, 7.5, 4, 4.5]
[float(i) for i in select_size] #All select_size values to float value
for size in json_data:
    if float(size['label']) in select_size: #For compare it i need float(size['label']) for convert to float.
        fsize.append(size['id']) #Add to list

print(fsize) #Print all list, i get only 28

deon cagadoes
  • 582
  • 2
  • 13