S
S
s19s932021-07-02 12:20:51
Python
s19s93, 2021-07-02 12:20:51

How to extract data from Json?

Kind !
How to extract data from json?
There is the following AIP
https://api.bittrex.com/api/v1.1/public/getmarkets
Data comes like this

<code>
{
  "success": true,
  "message": "''",
  "result": [
    {
      "MarketCurrency": "LTC",
      "BaseCurrency": "BTC",
      "MarketCurrencyLong": "Litecoin",
      "BaseCurrencyLong": "Bitcoin",
      "MinTradeSize": 0.01,
      "MarketName": "BTC-LTC",
      "IsActive": true,
      "IsRestricted": false,
      "Created": "2014-02-13T00:00:00",
      "Notice": "BTC-LTC",
      "IsSponsored": false,
      "LogoUrl": "https://storage.blob.core.windows.net/public/8637ccad-9e7f-45ac-8f03-a41b440e3911.png"
    }
  ]
}
</code>


Tell me how to pull out all the data "MarketCurrency": "LTC", "BaseCurrency": "BTC" and so on.
I am writing the following
import config #Конфигурация
import requests
import json

all = requests.get(config.url2)
all_data =json.loads(all.text)

for el in all_data :
    if el ['Currency']=='LTC':
        print (el[0]['Currency'])

We get the following error
TypeError: string indices must be integers It's a
bit unclear what the error is?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Ramis, 2021-07-02
@s19s93

import requests
res = requests.get('https://api.bittrex.com/api/v1.1/public/getmarkets').json()['result']
for i in res:
    print(i['MarketCurrency'])
    print(i['BaseCurrency'])
    print(i['MarketCurrencyLong'])
    print(i['BaseCurrencyLong'])
    print(i['MinTradeSize'])
    print(i['MarketName'])
    print(i['Created'])
    print('-------------------')

M
Morrowind, 2021-07-02
@hekkaaa

Hey!
Here is my example of similar api implementation

import requests #библиотека для чтения запросов

response = requests.get('https://1234.com/public', params=params) #запрос к api
workrequest = response.json() #забираем json из запроса. Обычно там список что то подобное

sql_json = [] #создаем свой список (list) пустой
    #циклом забираем данные в своей список.
    for i in range(len(workrequest)):
        sql_json.append(workrequest[i]['globalTradeID'])
        sql_json.append(workrequest[i]['tradeID'])
        upgradata_str = workrequest[i]['date']
        sql_json.append(updateloghourse(upgradata_str))
        sql_json.append(workrequest[i]['type'])
        sql_json.append(workrequest[i]['rate'])
        sql_json.append(workrequest[i]['amount'])
        sql_json.append(workrequest[i]['total'])

sqlADDinfoTable(sql_json,name_table) # В моем случае я отправляю данные сразу в таблицу SQL
        sql_json.clear() #Очищаю список для новой итерации цикла.

A
Alan Gibizov, 2021-07-02
@phaggi

data = '''{
  "success": true,
  "message": "''",
  "result": [
    {
      "MarketCurrency": "LTC",
      "BaseCurrency": "BTC",
      "MarketCurrencyLong": "Litecoin",
      "BaseCurrencyLong": "Bitcoin",
      "MinTradeSize": 0.01,
      "MarketName": "BTC-LTC",
      "IsActive": true,
      "IsRestricted": false,
      "Created": "2014-02-13T00:00:00",
      "Notice": "BTC-LTC",
      "IsSponsored": false,
      "LogoUrl": "https://storage.blob.core.windows.net/public/8637ccad-9e7f-45ac-8f03-a41b440e3911.png"
    }
  ]
}'''

import json

all_data = json.loads(data)
my_data = all_data['result'][0]
for key, value in my_data.items():
    if 'Currency' in key:
        print (value)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question