Skip to content
Advertisement

How to extract all symbol and last_price from this json?

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)

Advertisement

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)
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement