Getting error message: json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) while trying to access an API

Keywords: python json pandas

Question: 

I'm trying to build a dataframe of crypto prices from Huobi's API and I'm getting a JSON error message that I have never seen before.

I begin by getting all the symbols from the API, I next define the function to download the relevant price data, and then I begin looping through each symbol to begin building the dataframe. As some symbols are now obsolete, I test that data is actually available before trying to get it. The exact code I'm using is this:

import requests
import json
import datetime as dt
import pandas as pd

# GET LIST OF AVAILABLE SYMBOLS

def huobi_list():
    syms = json.loads(requests.get("https://api.huobi.pro/market/tickers").text)
    sym = syms['data']
    coins = []
    for i in sym:
        if i['symbol'][-3:] == 'btc':
            coins.append(i['symbol'])
    return coins


def get_huobi(symbol, interval):
    df = json.loads(requests.get('https://api.huobi.pro/market/history/kline?period=' + interval + '&size=1000&symbol=' + symbol).text)
    df = df['data']
    df = pd.DataFrame(df)
    df['Date'] = [dt.datetime.fromtimestamp(int(x)) for x in df.id]
    df.sort_values('Date', ascending=True, inplace=True)
    df.set_index('Date', inplace=True)
    return df

hu_coins = huobi_list()

hprices = pd.DataFrame()

# BEGIN LOOPING THROUGH TO GET THE DATA

for coin in hu_coins:
    print(coin)
    test = json.loads(
        requests.get('https://api.huobi.pro/market/history/kline?period=60min&size=1000&symbol=' + coin).text)
    if test['status'] != 'error':
        data = get_huobi(coin, '60min')
        df = pd.DataFrame(data['close'])
        df.reset_index(inplace=True)
        df.columns = ['Date', coin]
        df[coin] = pd.to_numeric(df[coin], errors='coerce')
        if hprices.empty:
            hprices = df
        else:
            hprices = hprices.merge(df[['Date', coin]], on='Date', how='left')

hprices.set_index('Date', inplace=True)
hprices.dropna(inplace=True, axis=1)

The error message I receive is the following:

json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

I've found there appears to be no rhyme or reason to this error message either as when I loop through, I get it to print out each symbol, to see if I could isolate if one particular symbol is causing the problem and it isn't the case. I've have actually once managed to get it to loop through the whole lot but that has been once in about 30 attempts.

Any help would be really appreciated. Cheers.

Answers: