I am making a request to an endpoint and getting a json response in the following format.
r = requests.get(url)
print(r.json())
{"context":"product=fhvh, price=25,product=vfjvnf,price=37,product=fvfv,price=3.....}
I want to loop over them and get the count of the products that have a price point of more than 22.
How can i get that? I tried to loop over the json response initially but i don’t believe that will work due to the format of the output. Is there a better solution? How can i convert the response to maybe a dict or tuple first? Can someone help me. Thank you.
>Solution :
This is the worst possible solution, it relies on the response always has a product and a price and can be very unreliable.
r = requests.get(url)
data = r.json()
context=data.get('context')
elements = context.split(',')
chunks=[elements[i:i + 2] for i in range(0, len(elements), 2)]
print(chunks)
[['product=fhvh', ' price=25'], ['product=vfjvnf', 'price=37'], ['product=fvfv', 'price=3']]
I’d look into data processing libraries used by Big Data like Pandas that might have a more reliable way to doing it.