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

Extract value from json response python?

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.

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

>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.

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