Ethereum: Upgrade portfolio JSON doesn’t get data
As an experienced creator, it is disappointing when a simple scenario does not work as expected. In this article, we will solve the problem of storage of Ethereum portfolio in the JSON file.
a problem
Code you have submitted before:
`Python
Import JSON
Datetime importing Datetime
Import request
From Binance.client Import Client
Initialize Binance API ID
Customer = Customer (Your_Btc_api_Key, You_Btc_API_SECRET)
Symbol = “Eth”
Exchange = “binance”
Def Updend_Portol ():
Portfolio = {}
Get current portfolio data
Answer = Customer.get_exchange_data (exchange = exchange, symbol = symbol)
Data = JSON.LADS (Response.Text)
Paired json data to update portfolio dictionary
For data assets [‘balance sheets’]:
If the property [“symbol”] == symbol:
Asset_amount = float (property [“amount”])
coin_name = property [“property”] [“name”]
Portfolio [coin_name] = property_amount
Save updated portfolio data in JSON file
With Open (Portfolio.json, W) as a file:
json.dump (portfolio, file)
Update_portfol ()
`
After adding this code, the Portfolio.json file is designed in the same directory. However, when you try to update it using JSON.LAD () or
json.dump () , nothing happens.
Solution
The problem is how we analyze JSON data from Binance API. We must have access to specific fields in each property object that corresponds to the coin we want to follow. Here is the updated version of the code:
Python
Import JSON
Datetime importing Datetime
Import request
From Binance.client Import Client
Initialize Binance API ID
Customer = Customer (Your_btc_api_Key, You_Btc_API_SECRET)
Symbol = “Eth”
Exchange = “binance”
Def Updend_Portol ():
Portfolio = {}
Get current portfolio data
Answer = Customer.get_exchange_data (exchange = exchange, symbol = symbol)
Data = JSON.LADS (Response.Text)
Paired json data to update portfolio dictionary
For data assets [‘balance sheets’]:
If the property [“symbol”] == symbol:
Asset_amount = float (property [“amount”])
coin_name = property [“property”] [“name”]
Portfolio [coin_name] = property_amount
Запазете актуализирани данни за портфолио във here json
С Open (“portfolio.json”, “w”) като Файл:
json.dump (портфолио, Файл)
Update_portfol ()
`
In this revised version:
- We use JSON.LAAD () rather than direct access to the variable “data”.
- We repeat each object of the property in the response and access to its attributes (for example, “property [” symbol “],” property [“sum”]) to determine which coin we want to follow.
for the use of case usage
To test this updated code, create a new file called “Main.py” with the following content:
`Python
Import JSON
Datetime importing Datetime
Import request
From Binance.client Import Client
Initialize Binance API ID
Customer = Customer (Your_btc_api_Key, You_Btc_API_SECRET)
Symbol = “BTC/USDT”
Exchange = “binance”
Def Main ():
Portfolio = {}
Get current portfolio data
Answer = Customer.get_exchange_data (exchange = exchange, symbol = symbol)
Data = JSON.LADS (Response.Text)
Paired json data to update portfolio dictionary
For data assets [‘balance sheets’]:
Ако актив [“символ”] == Символ:
Asset_amount = float (property [“amount”])
coin_name = property [“property”] [“name”]
Portfolio [coin_name] = property_amount
Save updated portfolio data in JSON file
With Open (Portfolio.json, W) as a file:
json.dump (portfolio, file)
Basic ()
`
Start the script using Python Main.py. This should create a new file, Portfolio.json in the same directory.