Ethereum: JSON portfolio update not getting data
As an experienced developer, it hurts me when a simple script doesn’t work as expected. In this article, we will solve the problem of storing Ethereum wallet data in a JSON file.
Problem
The code you provided earlier:
Python
import json
from datetime import datetime
import requests
from binance.client import client
Initialize Binance API credentials
client = Client(“YOUR_BTC_API_KEY”, “YOUR_BTC_API_SECRET”)
symbol=”ETH”
exchange = “binance”
definition of update_portfolio():
portfolio = {}
Get current portfolio data
response = client.get_exchange_data(exchange=exchange, symbol=symbol)
data = json.loads(response.text)
Analysis of JSON data to update the portfolio dictionary
for asset in data[“balances”]:
if active[“symbol”] == symbol:
asset_amount = float(asset[“amount”])
coin_name = asset[“asset”][“name”]
portfolio[coin_name] = asset_sum
Saving updated portfolio data to a JSON file
with open(“portfolio.json”, “w”) file:
json.dump(portfolio, file)
update_portfolio()
After adding this code, a portfolio.json
file is created in the same directory. However, when you try to update it using json.load()
or json.dump()
, nothing happens.
Solution
The problem lies in the way JSON data is parsed from the Binance API. We need to access certain fields in each asset object that correspond to the coin we want to track. Here is the updated version of the code:
Python
import json
from datetime import datetime
import requests
from binance.client import client
Initialize Binance API credentials
client = Client("YOUR_BTC_API_KEY", "YOUR_BTC_API_SECRET")
symbol="ETH"
exchange = "binance"
definition of update_portfolio():
portfolio = {}
Get current portfolio data
response = client.get_exchange_data(exchange=exchange, symbol=symbol)
data = json.loads(response.text)
Analysis of JSON data to update the portfolio dictionary
for asset in data["balances"]:
if active["symbol"] == symbol:
asset_amount = float(asset["amount"])
coin_name = asset["asset"]["name"]
portfolio[coin_name] = asset_sum
Saving updated portfolio data to a JSON file
with open("portfolio.json", "w") file:
json.dump(portfolio, file)
update_portfolio()
In this revised version:
- We use
json.load()
instead of directly accessing thedata
variable.
- We loop through each asset object in the response and access its attributes (eg
asset["symbol"]
,asset["amount"]
) to determine which coin we want to track.
Usage example
To test this updated code, create a new file named main.py
with the following content:
Python
import json
from datetime import datetime
import requests
from binance.client import client
Initialize Binance API credentials
client = Client(“YOUR_BTC_API_KEY”, “YOUR_BTC_API_SECRET”)
symbol = “BTC/USDT”
exchange = “binance”
definition of main():
portfolio = {}
Get current portfolio data
response = client.get_exchange_data(exchange=exchange, symbol=symbol)
data = json.loads(response.text)
Analysis of JSON data to update the portfolio dictionary
for asset in data[“balances”]:
if active[“symbol”] == symbol:
asset_amount = float(asset[“amount”])
coin_name = asset[“asset”][“name”]
portfolio[coin_name] = asset_sum
Saving updated portfolio data to a JSON file
with open(“portfolio.json”, “w”) file:
json.dump(portfolio, file)
main()
“
Run the script usingpython main.py. This should create a new
portfolio.json` file in the same directory.