Home > Net >  How to extract all symbol and last_price from this json?
How to extract all symbol and last_price from this json?

Time:05-26

I tried like this, but it doesn't work. It worked with an other json file (https://www.binance.com/fapi/v1/ticker/price) I think it's because the beginning and end is different. There is some parts before the bracket and after.

I got the error: symbol = (pair['symbol']) TypeError: string indices must be integers

import requests
import json

bybit = requests.get("https://api-testnet.bybit.com/v2/public/tickers")
e = bybit.json()

for pair in e:
    symbol = (pair['symbol'])
    price = (pair['last_price'])

    print (symbol)
    print (price)

CodePudding user response:

e contains some other information. The data that you want is in e['result']

import requests
import json

bybit = requests.get("https://api-testnet.bybit.com/v2/public/tickers")
e = bybit.json()

for pair in e['result']:
    symbol = (pair['symbol'])
    price = (pair['last_price'])

    print (symbol)
    print (price)
  • Related