0

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()

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

yes, it's using 1:1 mapping. thanks, it works now how I want.
1

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!

1 Comment

sure, if I were them, I'd use as you said but I could not change it unfortunately.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.