Python Forum
json api data parsing - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: json api data parsing (/thread-37001.html)



json api data parsing - elvis - Apr-21-2022

Having issue with this piece of code, its an api that has json text but its not readable.
in _get_mean_subclass
for prod, v in response_API.item():
Attribute error: 'list' object has no attribute 'item'

response_API = requests.get(
    f"https://api.bestbuy.com/v1/products(manufacturer=canon)?format=json&apiKey={cred.api_key}"
).json()


def _get_mean_subclass(response_API):
    r = defaultdict(list)
    for prod, v in response_API.items():
        # looking for keyword subclass or whatever its called
        r[prod].append(v["subclassId"])
    return {k: mean(v) for k, v in r.items()}


print(_get_mean_subclass(response_API["products"]))