gus
gus

Reputation: 1798

How do i select row json with the id

I have this

{"id":"141","bid":4.57000002,"ask":4.89999798},{"id":"345","bid":79933.93185001,"ask":92999.99999999}

i am using python how do i get just the bid or ask price with just an id?

Upvotes: 2

Views: 612

Answers (1)

Ozgur Vatansever
Ozgur Vatansever

Reputation: 52203

Assuming you have the a list of dictionaries in your JSON data:

def get_by_id(lst, id):
    for d in lst:
        if d.get('id') == str(id):
            return d
    return None

>>> import json

>>> data = '[{"id": "141", "bid": 4.57000002, "ask": 4.89999798}, {"id": "345", "bid": 79933.93185001, "ask": 92999.99999999}]'
>>> lst = json.loads(data)
>>> d = get_by_id(lst, 345)

>>> d['ask']
92999.99999999

>>> d['bid']
79933.93185001

Upvotes: 2

Related Questions