Wiktor Kisielewski
Wiktor Kisielewski

Reputation: 437

Get value from json - API

So I have an API conection established in python, everything is alright. My question is how to get to the value of 'avgEntryPrice'?

[{'account': 231584,'simpleCost': 0.0, 'simpleValue': 0.0, 'avgEntryPrice': 0.0, ...}]

this is returned by:

client.Position.Position_get(filter=json.dumps({'symbol': 'XBTUSD'})).result()

and here is swagger.json for API https://github.com/BitMEX/api-connectors/blob/master/swagger.json?source=post_page---------------------------

Thanks in advance!

Upvotes: 1

Views: 180

Answers (1)

Ramy M. Mousa
Ramy M. Mousa

Reputation: 5933

positions = json.loads(result)  # result is an array so you will either loop over it or select first item

avg_entry_price = positions[0]['avgEntryPrice']  # first record solution

or

for position in positions:  # iterative solution
    print(position['avgEntryPrice'])

More answers and details convert string to json

Upvotes: 1

Related Questions