0
votes

I've looked for similar questions, but I couldn't find anything what can help me. When i execute this code (which should work) I get "json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)"

TOKEN = '_EXAMPLE_TOKEN_'

def getPagedData(start,size):
    url = 'http://EXAMPLE_URL'.format(start, size)
    response = requests.get(url, headers={'token': TOKEN}, verify=False)
    return response

def getDataInBatches():
    start = 0
    size = 1000
    allData = []
    data = getPagedData(start, size)
    allData.extend(data.json()['data'])
    total = data.json()['count']
    for i in range(1, round(total/size) + 1):
        print(allData[len(allData) - 1])
        allData.extend(getPagedData(i * size, size).json()['data'])
    return allData

print(getDataInBatches())

Here full traceback:

Traceback (most recent call last):
  File "C:/Users/userxy/Documents/workProject/API/Get_Information.py", line 30, in <module>
    print(getDataInBatches())
  File "C:/Users/userxy/Documents/workProject/API/Get_Information.py", line 23, in getDataInBatches
    allData.extend(data.json()['data'])
  File "C:\Users\userxy\Anaconda3\lib\site-packages\requests\models.py", line 898, in json
    return complexjson.loads(self.text, **kwargs)
  File "C:\Users\userxy\Anaconda3\lib\json\__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "C:\Users\userxy\Anaconda3\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\userxy\Anaconda3\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

What can it be?

1
Print the response, or save it to file, before you try to decode it as json.balmy

1 Answers

0
votes

Make sure the response is in json format. The standard way to ensure the response is in json format is to add a headers={'Content-Type': 'application/json'} But to be sure, check the documentation of an API you're accessing.