0

I have a JSON file containing the list of price changes of all cryptocurrencies I want to extract all 'percentage' for all the coins.

Using the code below it throws TypeError: string indices must be integers (which I know is totally wrong, Basically trying to understand how can I search for percentage and get its value for all items)

with open('balance.txt') as json_file:
    data = json.load(json_file)

for json_i in data:
    print(json_i['priceChangePercent']) 

Any help is appreciated

I have attached the json file hereJSON FILE

Below is the sample of JSON file for those who dont want to open link

     {
   "ETH/BTC":{
      "symbol":"ETH/BTC",
      "timestamp":1630501910299,
      "datetime":"2021-09-01T13:11:50.299Z",
      "open":0.071579,
      "close":0.0744,
      "last":0.0744,
      "previousClose":0.071585,
      "change":0.002821,
      "percentage":3.941,
      "average":null,
      "baseVolume":178776.0338,
      "quoteVolume":13026.89979053,
      "info":{
         "symbol":"ETHBTC",
         "priceChange":"0.00282100",
         "priceChangePercent":"3.941",
         "count":"279051"
      }
   },
   "LTC/BTC":{
      "symbol":"LTC/BTC",
      "timestamp":1630501909389,
      "datetime":"2021-09-01T13:11:49.389Z",
      "open":0.003629,
      "close":0.00365,
      "last":0.00365,
      "previousClose":0.003629,
      "change":2.1e-05,
      "percentage":0.579,
      "average":null,
      "baseVolume":132964.808,
      "quoteVolume":485.12431556,
      "info":{
         "symbol":"LTCBTC",
         "priceChange":"0.00002100",
         "priceChangePercent":"0.579",
         
         "count":"36021"
      }
   },
   "BNB/BTC":{
      "symbol":"BNB/BTC",
      "timestamp":1630501910176,
      "datetime":"2021-09-01T13:11:50.176Z",
      "open":0.009848,
      "close":0.010073,
      "last":0.010073,
      "previousClose":0.009848,
      "change":0.000225,
      "percentage":2.285,
      "average":null,
      "baseVolume":220645.713,
      "quoteVolume":2187.75954249,
      "info":{
         "symbol":"BNBBTC",
         "priceChange":"0.00022500",
         "priceChangePercent":"2.285",
         
         "count":"130422"
      }
   },
2
  • Something I like to remind everyone who asks questions about "extracting ... from JSON": Unless you are having problems reading the JSON file itself, remember that JSON just deserialializes to standard Python data structures (typically a dict or list of dicts), so the question is not really a question about JSON, but about how to manipulate Python data structures (in this case extracting values for a specific key in a list of dicts, for which you can find many existing answers on SO. Commented Sep 1, 2021 at 13:55
  • In this case something like percentages = [d['percentage'] for d in data.values() if 'percentage' in d] Commented Sep 1, 2021 at 13:56

3 Answers 3

2

If it is single dictionary, it could be done the following way:

data['LTC/BTC']['info']['priceChangePercent']
Sign up to request clarification or add additional context in comments.

Comments

1

Extract it using list comprehension.

percentage_list = [value['percentage'] for value in data.values()]
priceChangePercent_list = [value['info']['priceChangePercent'] for value in data.values()]

print(percentage_list)
print(priceChangePercent_list)
[3.941, 0.579, 2.285]
['3.941', '0.579', '2.285']

Comments

1

try this bro

t = []
for key, value in a.items():
  if "info" in value and "priceChangePercent" in value["info"]:
    t.append(value["info"]["priceChangePercent"])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.