close

[Solved] json.decoder.JSONDecode: Expecting value: line 1 column 1 (char 0)

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error json.decoder.JSONDecode: Expecting value: line 1 column 1 (char 0) in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How json.decoder.JSONDecode: Expecting value: line 1 column 1 (char 0) Error Occurs?

Today I get the following error json.decoder.JSONDecode: Expecting value: line 1 column 1 (char 0) in python.

How To Solve json.decoder.JSONDecode: Expecting value: line 1 column 1 (char 0) Error ?

  1. How To Solve json.decoder.JSONDecode: Expecting value: line 1 column 1 (char 0) Error ?

    To Solve json.decoder.JSONDecode: Expecting value: line 1 column 1 (char 0) Error Check the response data-body, whether actual data is present and a data-dump appears to be well-formatted.

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

    To Solve json.decoder.JSONDecode: Expecting value: line 1 column 1 (char 0) Error Check the response data-body, whether actual data is present and a data-dump appears to be well-formatted.

Solution 1


Your code produced an empty response body, you’d want to check for that or catch the exception raised. It is possible the server responded with a 204 No Content response, or a non-200-range status code was returned (404 Not Found, etc.). Check for this.

Note:

  • There is no need to use simplejson library, the same library is included with Python as the json module.
  • There is no need to decode a response from UTF8 to unicode, the simplejson / json .loads() method can handle UTF8 encoded data natively.
  • pycurl has a very archaic API. Unless you have a specific requirement for using it, there are better choices.

Either the requests or httpx offers much friendlier APIs, including JSON support. If you can, replace your call with:

import requests

response = requests.get(url)
response.raise_for_status()  # raises exception when not a 2xx response
if response.status_code != 204:
    return response.json()

Of course, this won’t protect you from a URL that doesn’t comply with HTTP standards; when using arbirary URLs where this is a possibility, check if the server intended to give you JSON by checking the Content-Type header, and for good measure catch the exception:

if (
    response.status_code != 204 and
    response.headers["content-type"].strip().startswith("application/json")
):
    try:
        return response.json()
    except ValueError:
        # decide how to handle a server that's misbehaving to this extent

Solution 2

Check the response data-body, whether actual data is present and a data-dump appears to be well-formatted.

In most cases your json.loads– JSONDecodeError: Expecting value: line 1 column 1 (char 0) error is due to :

  • non-JSON conforming quoting
  • XML/HTML output (that is, a string starting with <), or
  • incompatible character encoding

Ultimately the error tells you that at the very first position the string already doesn’t conform to JSON.

As such, if parsing fails despite having a data-body that looks JSON like at first glance, try replacing the quotes of the data-body:

import sys, json
struct = {}
try:
  try: #try parsing to dict
    dataform = str(response_json).strip("'<>() ").replace('\'', '\"')
    struct = json.loads(dataform)
  except:
    print repr(resonse_json)
    print sys.exc_info()

Note: Quotes within the data must be properly escaped

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read