esTERY
esTERY

Reputation: 31

How to resolve simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)?

I'm receiving the following error randomly when making a GET request.

simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

I am not receiving a rate limit error from the API, so I'm not sure why this error occurs. I assume is because the JSON object is empty.

import requests


try:
    r = requests.get(url=url)
except requests.exceptions.RequestException as e:
    logging.error(e)
else:
    if r.status_code == 200:
        data = r.json()

The response is usually like this

[['string', 1.2, 20.4, 8.6, 9.3, 5.6, 6.5, 8.6, 7.8, 8.8, 8.3]]

Upvotes: 0

Views: 1731

Answers (1)

Meny Issakov
Meny Issakov

Reputation: 1421

It seems that your response isn't a JSON one, but a string.

If you're certain the output response should be JSON (though not structured as one), you can try using ast module to parse it

import requests
import ast


try:
    r = requests.get(url=url)
    if r.ok:
        data = ast.literal_eval(r.content) if r.content else []
except Exception as e:
    logging.error(e)

Upvotes: 1

Related Questions