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 decode a response from UTF8 to Unicode, the
json.loads()method can handle UTF8-encoded data natively.pycurlhas a very archaic API. Unless you have a specific requirement for using it, there are better choices.
Either requests or httpx offer much friendlier APIs, including JSON support.
If you can, replace your call with the following httpx code:
import httpx
response = httpx.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 arbitrary 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
Answer from Martijn Pieters on Stack OverflowYour 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 decode a response from UTF8 to Unicode, the
json.loads()method can handle UTF8-encoded data natively.pycurlhas a very archaic API. Unless you have a specific requirement for using it, there are better choices.
Either requests or httpx offer much friendlier APIs, including JSON support.
If you can, replace your call with the following httpx code:
import httpx
response = httpx.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 arbitrary 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.load(json_file_path)
JSON doc:
{
"Data" : [
"input1",
"input2",
"input3",
"input4"
]
}Python code:
import json
with open("data.json", 'r') as f:
json_data = json.load(f)
print(json_data)Error message:
sh-5.1$ /bin/python /home/USER/Documents/Python/learning
Traceback (most recent call last):
File "/home/USER/Documents/Python/learning", line 27, in <module>
json_data = json.load(f)
File "/usr/lib/python3.9/json/__init__.py", line 293, in load
return loads(fp.read(),
File "/usr/lib/python3.9/json/__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.9/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.9/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)Disproved theories: Improper syntax (python), document not located, bad formatting (json), accidental empty space in json.
I get this error each time I try to use "json.load" so long as it has a valid parameter input. Whether read or write; error. I'm using Visual Studio Code on Linux Mint in case that matters.
Thanks for your time! I'll be happy to answer questions in the morning when I wake up.
EDIT: Clarity, and reddit dislikes my use of backticks. Manually tabbed code.
EDIT: This code SHOULD work. It does on other computers. But it doesn't on mine, and I don't know why nor how to fix it.