Home > Software engineering >  Extract value from json response python?
Extract value from json response python?

Time:05-18

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.

CodePudding user response:

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.

Update

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_list = []
for item in chunks:
     obj1=item[0].split('=')
     obj2=item[1].split('=')
     product = { obj1[0].strip(): obj1[1].strip(), obj2[0].strip(): float(obj2[1].strip())}
     product_list.append(product)

print(product_list)

for product in product_list:
     if product.get('price') > 22.0:
             print(product)

CodePudding user response:

Your issue is that the payload you have isn't JSON but some alternative format. My advice would be to convert each entry in context into an object:

class Product:
    def __init__(self, name, price):
        self.name = name
        self.price = float(price)

def parse_data(resp):

    # First, read in the JSON
    raw_data = r.json()
 
    # Next, split the context field in the JSON into a list-of-tuples
    # where each item represents a key and value.
    parsed_data = [tuple(item.strip().split('=')) for item in raw_data["context"].split(',')]
   
    # Finally, convert each product to our type and return the list of them
    # This assumes that the payload contains a product followed by a price
    return [Product([i][1], y[i   1][1]) for i in range(0, len(y), 2)]

r = requests.get(url)
filtered = [product for product in parse_data(r) if product.price > 22]

This code obeys separation of concerns and produces the output you want (all products with a price greater than 22). That being said, this relies heavily on assumptions about what is returned from the API you're requesting. You'll want to add data checks to ensure that you're not getting bad data.

  • Related