M
M
Morrdor2021-11-25 16:04:49
Python
Morrdor, 2021-11-25 16:04:49

How to iterate json array in python?

there is a response from api

data{
  info{
         product1{
            prodinfo:1
         }
        product2{
           prodinfo:2
         }
        product1{
           prodinfo:3
         }
    }

}


How do I get product1,2,3 and prodinfo?
I know that I need to iterate over the array. But is it possible to somehow iterate without nested arrays?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alexanderzanin, 2021-11-25
@alexanderzanin

resp = {
    'data': {
        'info': {
            'product_1': {
                'prodinfo': 1
            },
            'product_2': {
                'prodinfo': 2
            },
            'product_3': {
                'prodinfo': 3
            }
        }
    }
}


products_key = [x for x in resp['data']['info'].keys()]
print(products_key)
products = []
for key in products_key:
    products.append(resp['data']['info'][key])

print(products)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question