I am getting error Expecting value: line 1 column 1 (char 0)
when trying to decode JSON.
The URL I use for the API call works fine in the browser, but gives this error when done through a curl request. The following is the code I use for the curl request.
The error happens at return simplejson.loads(response_json)
response_json = self.web_fetch(url) response_json = response_json.decode('utf-8') return json.loads(response_json) def web_fetch(self, url): buffer = StringIO() curl = pycurl.Curl() curl.setopt(curl.URL, url) curl.setopt(curl.TIMEOUT, self.timeout) curl.setopt(curl.WRITEFUNCTION, buffer.write) curl.perform() curl.close() response = buffer.getvalue().strip() return response
Traceback:
File "/Users/nab/Desktop/myenv2/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response 111. response = callback(request, *callback_args, **callback_kwargs) File "/Users/nab/Desktop/pricestore/pricemodels/views.py" in view_category 620. apicall=api.API().search_parts(category_id= str(categoryofpart.api_id), manufacturer = manufacturer, filter = filters, start=(catpage-1)*20, limit=20, sort_by='[["mpn","asc"]]') File "/Users/nab/Desktop/pricestore/pricemodels/api.py" in search_parts 176. return simplejson.loads(response_json) File "/Users/nab/Desktop/myenv2/lib/python2.7/site-packages/simplejson/__init__.py" in loads 455. return _default_decoder.decode(s) File "/Users/nab/Desktop/myenv2/lib/python2.7/site-packages/simplejson/decoder.py" in decode 374. obj, end = self.raw_decode(s) File "/Users/nab/Desktop/myenv2/lib/python2.7/site-packages/simplejson/decoder.py" in raw_decode 393. return self.scan_once(s, idx=_w(s, idx).end()) Exception Type: JSONDecodeError at /pricemodels/2/dir/ Exception Value: Expecting value: line 1 column 1 (char 0)
JSONDecodeError: Expecting value: line 1 column 1 (char 0)" occurs when we try to parse something that is not valid JSON as if it were. To solve the error, make sure the response or the file is not empty or conditionally check for the content type before parsing.
How can JSONDecodeError: Expecting value: line 1 column 1 (char 0) occur? JSONDecodeError means there is an incorrect JSON format being followed. For instance, the JSON data may be missing a curly bracket, have a key that does not have a value, and data not enclosed within double-quotes or some other syntactic error.
You can resolve this error by checking the JSON file or string for valid content and using an if-statement when making a RESTful call to ensure the status code is 200 and the content type is application/json .
The Python "json. decoder. JSONDecodeError: Extra data" occurs when we try to parse multiple objects without wrapping them in an array. To solve the error, wrap the JSON objects in an array or declare a new property that points to an array value that contains the objects.
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
Be sure to remember to invoke json.loads()
on the contents of the file, as opposed to the file path of that JSON:
json_file_path = "/path/to/example.json" with open(json_file_path, 'r') as j: contents = json.loads(j.read())
I think a lot of people are guilty of doing this every once in a while (myself included):
contents = json.loads(json_file_path)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With