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]]

1
  • 1
    That response is not a valid JSON, JSON has no strings with single-quotes (') in them. Commented Mar 14, 2020 at 23:31

1 Answer 1

1
+50

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.