How to extract all symbol and last_price from this json?

944 views Asked by At

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)
1

There are 1 answers

0
Ahmad On BEST ANSWER

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)