Skip to content Skip to sidebar Skip to footer

Parsing Json.dumps - Python

I have a call to an API that returns the following JSON: { 'trades': [ { 'stopLoss': 154.79, 'takeProfit': 151.79, 'price': 153.784, 'side':

Solution 1:

You can use json.loads() to parse the json.

import json
response = '{"trades": [{"stopLoss": 154.79, "takeProfit": 151.79, "price": 153.784, "side": "sell", "trailingStop": 0, "instrument": "GBP_JPY", "time": "2016-06-21T18:20:24.000000Z", "units": 25, "id": 10297636517, "trailingAmount": 0}]}'

def transactions():
    json_tree = json.loads(response)
    trade_list = json_tree['trades']
    trade = trade_list[0]
    return (trade['id'], trade['price'])

print transactions()

Solution 2:

Figured out the answer. I was trying just ['trades']['id'] and not ['trades'][0]['id'].. thanks

Post a Comment for "Parsing Json.dumps - Python"