0
I’m trying to extract the "balance" from a JSON. I would like to know how I can do this in a direct way, without receiving this complete message, which ends up being giant, but rather extract only the value I want.
import websocket
import json
def on_open(ws):
json_data = json.dumps({"authorize": "Vn55jnImK7UzcYr"}) # valor apenas de exemplo
ws.send(json_data)
def on_message(ws, message):
print('Dados: %s' % message)
if __name__ == "__main__":
apiUrl = "wss://ws.binaryws.com/websockets/v3?app_id=1089"
ws = websocket.WebSocketApp(apiUrl, on_message = on_message, on_open = on_open)
ws.run_forever()
The printing in IDLE is according to the image below that I cut due the data, but I would like to extract this balance value. For this I would have to manipulate the string? The problem of manipulating the string by taking the specific positions, is that this value can go from 0 to 999999... Then the position would always change. Or has some way to directly receive this amount?
I tried to do so a = message b = json.loads(a) print(b['balance'])
– Luiz Mendes
That way you put it worked very well. Just a doubt, would be able to extract only the number without it [' ']? Or I’ll still have to manipulate the string?
– Luiz Mendes
It’s a list. So you can iterate on it. I’ll edit the answer. Anyway, look at the @Paulo Marques answer that presented a simpler solution.
– Lucas