Arnold Wolfstein
Arnold Wolfstein

Reputation: 1

Python Json requests url

I'm using JSON and requests for getting some prices for some thresholds from an URL.

How can I get the price for a specific symbol, for instance, the price of Z1?

Here is the output of url:

[{"symbol":"E1","price":"89"},{"symbol":"E2","price":"87"},{"symbol":”Z1","price":"73"},{"symbol":"D4","price":"47"}]

My code goes like:

def prices():
    priceTick = requests.get('https://www.examples/prices')
    return priceTick.json()

Upvotes: 0

Views: 67

Answers (2)

AndyPi
AndyPi

Reputation: 86

Also you could be encountering an issue because you have a curly quote before Z1 - change this to a standard ", that can easily cause issues!

Upvotes: 1

alecxe
alecxe

Reputation: 473753

I would transform that into a more usable data structure to provide fast lookups - a dictionary:

price_ticks = {item["symbol"]: item["price"] for item in prices()}
print(price_ticks["Z1"])

This, of course, assumes each symbol has a single price - 1 to 1 mapping.

Upvotes: 1

Related Questions