Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Looping throuh json

I’am trying to add json data to the lists.
Json looks like this:

[{'genus': 'Musa', 'name': 'Banana', 'id': 1, 'family': 'Musaceae', 'order': 'Zingiberales', 'nutritions': {'carbohydrates': 22, 'protein': 1, 'fat': 0.2, 'calories': 96, 'sugar': 17.2}}]

But with my function, iam getting only those objects:

genus': 'Musa', 'name': 'Banana', 'id': 1, 'family': 'Musaceae', 'order': 'Zingiberales'

Can’t get anything from ‘nutritions’.
Adding code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import requests
import json


name = []
id = []
family = []
genus = []
order = []
carbohydrates = []
protein = []
fat = []
calories = []
sugar = []

def scrape_all_fruits():
    data_list = []
    try:
        for ID in range(1, 10):
            url = f'https://www.fruityvice.com/api/fruit/{ID}'
            response = requests.get(url)
            data = response.json()
            data_list.append(data)
    except:
        pass
    return data_list


def listify(fruit_stats):
    alist = json.dumps(scrape_all_fruits())
    jsonSTr = json.loads(alist)
    for i in jsonSTr:
        try:
            name.append(i['name'])
            id.append(i['id'])
            family.append(i['family'])
            genus.append(i['genus'])
            order.append(i['order'])
            carbohydrates.append(i['carbohydrates'])
            protein.append(i['protein'])
            # fat.append(i['fat'])
            calories.append(i['calories'])
            sugar.append(i['sugar'])
            for nutrs in i:
                fat.append(nutrs.a['fat'])
        except:
            pass
    return fruit_stats


print(listify(fat))

Can anyone explain to me what iam doing wrong ? Thank You in advance.

>Solution :

jsonSTr in your code is a dictionary. By default looping over a dictionary returns its keys.

You can fix this by either looking up via the key you receive:

name.append(jsonSTr[i]["name"])

or by looping over the values:

for i in jsonSTr.values():

if you need both the key and the value you can use the items() method.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading