Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
546 views
in Technique[技术] by (71.8m points)

Python - Reformat a JSON API response from Binance

So, according to Python-Binance's documentation, the thing I am getting back is a dictionary JSON response. When I get the response it looks like this:

{'symbol': 'BTCUSDT', 'price': '37256.90000000'}

What I want to do is take that and reformat it so it looks like this:

'$BTC @ 37256.900'

Basically removing the braces and all the other garbage that isn't needed.

The code I was using from the Binance API with Python-Binance and the way I was printing it:

price = client.get_symbol_ticker(symbol="BTCUSDT")

from crypto_tracker import price
print(price)

Is this possible at all?

question from:https://stackoverflow.com/questions/66057693/python-reformat-a-json-api-response-from-binance

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Python has a great library called json to help you take a json string and create a python dict from it.

From your example I have created the following code:

import json

## Given example string
json_string = '{"symbol": "BTCUSDT", "price": "37256.90000000"}'

## Create a python dict from json string using json library json.loads
json_dict = json.loads(json_string)

## What the new python dict looks like:
## json_dict = {'symbol': 'BTCUSDT', 'price': '37256.90000000'}

## Print your expected output
print(f'$BTC @ {json_dict["price"]}')

Print Output:

$BTC @ 37256.90000000

Using the python "json" library will automatically convert the json string to a python dict which you can then use any way you like (i.e. automatically removing all the extra "braces and ... other garbage that isn't needed.").

Let me know if this helped and if you have any questions!

Edit:

Per more back and forth in comments I took a look at the Binance API. The API endpoint your using is:

GET /api/v3/ticker/price

To get information from this you would need to make an HTTP request to:

https://api.binance.com/api/v3/ticker/price

Here is example working code:

import requests
import json

## Send a GET request to binance api
## Since this is a get request no apikey / secret is needed
response = requests.get("https://api.binance.com/api/v3/ticker/price?symbol=BTCUSDT")

## Print the text from the get request
## Output: {"symbol":"BTCUSDT","price":"37211.17000000"}
print(response.text)

## Response.text is a string so we can use json.loads on it
ticket = json.loads(response.text)

## Now that ticket is a python dict of the json string
## response from binance we can print what you want
print(f'$BTC @ {ticket["price"]}')

## Output: $BTC @ 37221.87000000

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...