Home > database >  How to loop thru JSON list and save data in Django
How to loop thru JSON list and save data in Django

Time:04-12

I am trying loop thru JSON list data and save into the variable 'nfts'.

Exception Value: string indices must be integers

Traceback: nft_data_id = item['nfts'][item]['nft_data_id'],

JSON response: {"result":{"page_count":3041,"nfts":[{"nft_id":"#-#-#-#","nft_data_id":"#-#-#-#", ...

views.py Code:

def market_data(request):
    URL = '...'
    response = requests.get(URL).json()
    nfts = response['result']
    for item in nfts:
        nft_data = NFT_attributes(
            nft_data_id = item['nfts'][item]['nft_data_id'],
            ...

CodePudding user response:

you're iterating over an object instead of an array. kindly try below example

def market_data(request):
    URL = '...'
    response = requests.get(URL).json()
    nfts = response['result']['nfts']
    for nft in nfts:
        nft_data = NFT_attributes(
            nft_data_id = nft['nft_data_id'],
            ...
  • Related