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:
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.
CodePudding user response:
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.
CodePudding user response:
IMO, your code is too complicated. If you have a nested dictionary, you can flatten it with a special function (example from here).
from collections.abc import MutableMapping
import pandas as pd
import json
def flatten_dict(d: MutableMapping, sep: str= ".") -> MutableMapping:
[flat_dict] = pd.json_normalize(d, sep=sep).to_dict(orient="records")
return flat_dict
Data = []
test_response = '[{"genus": "Musa", "name": "Banana", "id": 1, "family": "Musaceae", "order": "Zingiberales", "nutritions": {"carbohydrates": 22, "protein": 1, "fat": 0.2, "calories": 96, "sugar": 17.2}}]'
ResponseJSON = json.loads(test_response)
Data.append(flatten_dict(ResponseJSON[0]))
print(json.dumps(Data, indent=4))
Output:
[
{
"genus": "Musa",
"name": "Banana",
"id": 1,
"family": "Musaceae",
"order": "Zingiberales",
"nutritions.carbohydrates": 22,
"nutritions.protein": 1,
"nutritions.fat": 0.2,
"nutritions.calories": 96,
"nutritions.sugar": 17.2
}
]
What to do further, is up to you, but the output structure is good for pandas tables, which can be easily manipulated:
Table = pd.DataFrame(Data)
Hope that helps!