Json decode error python requests

JSONDecodeError: Expecting value: line 1 column 1 (char 0) occurs while working with JSON (JavaScript Object Notation) format. You might be storing some

JSONDecodeError: Expecting value: line 1 column 1 (char 0) occurs while working with JSON (JavaScript Object Notation) format. You might be storing some data or trying to fetch JSON data from an API(Application Programming Interface). In this guide, we will discuss where it can creep in and how to resolve it.

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.

A JSON file example
A JSON file example

Generally, the error expecting value: line 1 column 1 (char 0) error can occur due to the following reasons.

  • Non-JSON conforming quoting
  • Empty JSON file
  • XML output (that is, a string starting with <)

Let’s elaborate on the points stated above:

1. Non-JSON conforming quoting

JSON or JavaScript Object Notation has a similar format to that of the python dictionary datatype. A dictionary requires a key or value to be enclosed in quotes if it is a string. Similarly, the JSON standard defines that keys need not be a string. However, keys should always be enclosed within double quotes. Not following this standard can also raise an error.

Keys must be double-quoted in JSON
Keys must be double-quoted in JSON

“Other Commands Don’t Work After on_message” in Discord Bots

2. Empty JSON file

For this example, we have taken an empty JSON file named test.py and another file named test.py, which contains the code given below. Using context manager, we open the JSON file in reading mode and load it using the load method. However, an error is thrown because the JSON file is empty.

import json

with open("test.json", "r") as file:
    data = json.load(file)

print("Data retrieved")
Empty JSON file returns an error
Empty JSON file causes an error

3. XML output (that is, a string starting with <)

The Extensible Markup Language, or simply XML, is a simple text-based format for representing structured information: documents, data, configuration, books, transactions, invoices, and much more. Similar to JSON, it is an older way of storing data. Earlier APIs used to return data in XML format; however, JSON is nowadays the preferred choice. Let’s see how we can face the expecting value: line 1 column 1 (char 0) type error in this case.

<part number="1976">
  <name>Windscreen Wiper</name>
  <description>The Windscreen wiper
    automatically removes rain
    from your windscreen, if it
    should happen to splash there.
    It has a rubber <ref part="1977">blade</ref>
    which can be ordered separately
    if you need to replace it.
  </description>
</part>
import json
with open("test.xml", "r") as file:
    data = json.load(file)
print("Data retrieved")

Let’s break down what is happening here.

  • For ease of example, suppose API returns an XML format data, as shown above.
  • Now, when we try to load that response from API, we will get a type error.
XML response causes an error
XML response causes an error

Resolving JSONDecodeError: Expecting value: line 1 column 1 (char 0)

1. Solution for Non-JSON conforming quoting

To resolve the type error in this case, you need to ensure that the keys and values are enclosed within the double quotes. This is necessary because it is a part of JSON syntax. It is important to realize that JSON only uses double quotes, not single quotes.

Use double quotes for enclosing keys and values
Use double quotes for enclosing keys and values

2. Solution for empty JSON file

The solution for this is self-evident in the name. To resolve the type error, in this case, make sure that the JSON file or the response from an API is not empty. If the file is empty, add content to it, and for a response from an API, use try-except to handle the error, which can be empty JSON or a 404 error, for instance.

import json
import requests


def main():
    URL = "https://api.dictionaryapi.dev/api/v2/enties/en/"
    word = input("Enter a word:")
    data = requests.get(url + word)
    data = data.text
    try:
        data_json = json.loads(data)
        print(data_json)
    except json.JSONDecodeError:
        print("Empty response")


if __name__ == "__main__":
    main()

The above code takes in a word and returns all the information related to it in a JSON format. Now in order to show how we can handle the Expecting value: line 1 column 1 (char 0) type error, we have altered the URL. entries have been changed to enties. Therefore we will get an invalid response which will not be of the JSON format. However, this is merely done to imitate the error when you might get an invalid response from an API.

  • Now, if you try to run the code above, you will be prompted to enter a word. The response is saved into the data variable and later converted to a string.
  • However, in the try block json.loads method, which parses JSON string to python dictionary raises an error.
  • This is because the response sent by API is not of JSON format, hence can’t be parsed, resulting in JSONDecodeError.
  • JSONDecodeError gets handled by the try-except block, and a “Response content is not valid JSON” gets printed as an outcome.
Response content is not valid JSON
The response content is not valid JSON
Response when everything runs correctly.
Response when everything runs correctly.

3. Solution for XML output(that is, a string starting with <)

To avoid type errors resulting from an XML format, we will convert it to a JSON format. However, firstly install this library.

pip install xmltodict
import json
import xmltodict

with open("test.xml", "r") as file:
    data = xmltodict.parse(file.read())
    file.close()

    json_data = json.dumps(data)
    with open("t.json", "w") as json_file:
        json_file.write(json_data)
        json_file.close()
print("Data retrieved")
print(data)

Let’s elaborate on the code above:

  • We have imported two libraries, namely JSON and xmltodict.
  • Using the context manager with, XML file test.xml is opened in read mode. Thereafter using the xmltodict parse method, it is converted to a dictionary type, and the file is closed.
  • json.dumps() takes in a JSON object and returns a string of that object.
  • Again using context manager with, a JSON file is created, XML data that was converted to a JSON string is written on it, and the file is closed.
xml data converted into json format
XML data converted into JSON format

JSONDecodeError: Expecting value: line 1 column 1 (char 0) Django

This issue is caused by the failure of Pipenv 2018.10.9. To resolve this issue, use Pipenv 2018.5.18. For more, read here.

Are you facing this error in Flask?

Many times, when you receive the data from HTTP requests, they are fetched as bytes. So, if you face JSONDecodeError, you might be dealing with a JSON in bytes. First, you need to decode the JSON by using response.decode('utf-8'). This will create a string, and then you can parse it.

FAQs

How to parse JSON in python?

json.loads() method can be used to parse JSON in python. For instance:
import json
json_string = '{"a":"1", "b":"2", "c":"3"}'
json_to_dict = json.loads(json_string)
print(json_to_dict)
print(type(json_to_dict))

How to detect empty file/string before parsing JSON?

import os
file_path = "/home/nikhilomkar/Desktop/test.json"
print("File is empty!" if os.stat(file_path).st_size == 0 else "File isn't empty!"
)
The following code checks if the file is empty and prints the File is empty! If true, else, the File isn’t empty!.

Conclusion JSONDecodeError: Expecting value: line 1 column 1 (char 0)

The following article discussed the JSONDecodeError: Expecting value: line 1 column 1 (char 0). This error is due to various decoding and formatting errors. We looked at likely situations where it can occur and how to resolve it.

Trending Python Articles

  • “Other Commands Don’t Work After on_message” in Discord Bots

    “Other Commands Don’t Work After on_message” in Discord Bots

    February 5, 2023

  • Botocore.Exceptions.NoCredentialsError: Unable to Locate Credentials

    Botocore.Exceptions.NoCredentialsError: Unable to Locate Credentials

    by Rahul Kumar YadavFebruary 5, 2023

  • [Resolved] NameError: Name _mysql is Not Defined

    [Resolved] NameError: Name _mysql is Not Defined

    by Rahul Kumar YadavFebruary 5, 2023

  • Best Ways to Implement Regex New Line in Python

    Best Ways to Implement Regex New Line in Python

    by Rahul Kumar YadavFebruary 5, 2023

Содержание

  1. [Fixed] JSONDecodeError: Expecting Value: Line 1 column 1 (char 0)
  2. How can JSONDecodeError: Expecting value: line 1 column 1 (char 0) occur?
  3. 2. Empty JSON file
  4. 3. XML output (that is, a string starting with
  5. Resolving JSONDecodeError: Expecting value: line 1 column 1 (char 0)
  6. 1. Solution for Non-JSON conforming quoting
  7. 2. Solution for empty JSON file
  8. 3. Solution for XML output(that is, a string starting with
  9. JSONDecodeError: Expecting value: line 1 column 1 (char 0) Django
  10. Are you facing this error in Flask?
  11. Conclusion JSONDecodeError: Expecting value: line 1 column 1 (char 0)
  12. Cope with JSONDecodeError in requests.get().json() in Python 2 and 3
  13. Comments
  14. Python JSONDecodeError: Expecting value: line 1 column 1 (char 0)
  15. Table of contents
  16. JSONDecodeError: Expecting value: line 1 column 1 (char 0)
  17. Example #1: Incorrect use of json.loads()
  18. Solution
  19. Example #2: Empty JSON file
  20. Solution
  21. Example #3: Response Request
  22. Summary
  23. Quickstart¶
  24. Make a Request¶
  25. Passing Parameters In URLs¶
  26. Response Content¶
  27. Binary Response Content¶
  28. JSON Response Content¶
  29. Raw Response Content¶
  30. Custom Headers¶
  31. More complicated POST requests¶
  32. POST a Multipart-Encoded File¶
  33. Response Status Codes¶
  34. Response Headers¶
  35. Cookies¶
  36. Redirection and History¶
  37. Timeouts¶
  38. Errors and Exceptions¶

[Fixed] JSONDecodeError: Expecting Value: Line 1 column 1 (char 0)

JSONDecodeError: Expecting value: line 1 column 1 (char 0) occurs while working with JSON (JavaScript Object Notation) format. You might be storing some data or trying to fetch JSON data from an API(Application Programming Interface). In this guide, we will discuss where it can creep in and how to resolve it.

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.

Generally, the error expecting value: line 1 column 1 (char 0) error can occur due to the following reasons.

  • Non-JSON conforming quoting
  • Empty JSON file
  • XML output (that is, a string starting with 1. Non-JSON conforming quoting

JSON or JavaScript Object Notation has a similar format to that of the python dictionary datatype. A dictionary requires a key or value to be enclosed in quotes if it is a string. Similarly, the JSON standard defines that keys need not be a string. However, keys should always be enclosed within double quotes. Not following this standard can also raise an error.

2. Empty JSON file

For this example, we have taken an empty JSON file named test.py and another file named test.py, which contains the code given below. Using context manager, we open the JSON file in reading mode and load it using the load method. However, an error is thrown because the JSON file is empty.

3. XML output (that is, a string starting with

The Extensible Markup Language, or simply XML, is a simple text-based format for representing structured information: documents, data, configuration, books, transactions, invoices, and much more. Similar to JSON, it is an older way of storing data. Earlier APIs used to return data in XML format ; however, JSON is nowadays the preferred choice. Let’s see how we can face the expecting value: line 1 column 1 (char 0) type error in this case.

Let’s break down what is happening here.

  • For ease of example, suppose API returns an XML format data, as shown above.
  • Now, when we try to load that response from API, we will get a type error.

Resolving JSONDecodeError: Expecting value: line 1 column 1 (char 0)

1. Solution for Non-JSON conforming quoting

To resolve the type error in this case, you need to ensure that the keys and values are enclosed within the double quotes. This is necessary because it is a part of JSON syntax. It is important to realize that JSON only uses double quotes, not single quotes.

2. Solution for empty JSON file

The solution for this is self-evident in the name. To resolve the type error, in this case, make sure that the JSON file or the response from an API is not empty. If the file is empty, add content to it, and for a response from an API, use try-except to handle the error, which can be empty JSON or a 404 error, for instance.

The above code takes in a word and returns all the information related to it in a JSON format. Now in order to show how we can handle the Expecting value: line 1 column 1 (char 0) type error, we have altered the URL. entries have been changed to enties. Therefore we will get an invalid response which will not be of the JSON format. However, this is merely done to imitate the error when you might get an invalid response from an API.

  • Now, if you try to run the code above, you will be prompted to enter a word. The response is saved into the data variable and later converted to a string.
  • However, in the try block json.loads method, which parses JSON string to python dictionary raises an error.
  • This is because the response sent by API is not of JSON format, hence can’t be parsed, resulting in JSONDecodeError.
  • JSONDecodeError gets handled by the try-except block, and a “Response content is not valid JSON” gets printed as an outcome.

3. Solution for XML output(that is, a string starting with

To avoid type errors resulting from an XML format, we will convert it to a JSON format. However, firstly install this library.

Let’s elaborate on the code above:

  • We have imported two libraries, namely JSON and xmltodict.
  • Using the context manager with, XML file test.xml is opened in read mode. Thereafter using the xmltodict parse method, it is converted to a dictionary type, and the file is closed.
  • json.dumps() takes in a JSON object and returns a string of that object.
  • Again using context manager with, a JSON file is created, XML data that was converted to a JSON string is written on it, and the file is closed.

JSONDecodeError: Expecting value: line 1 column 1 (char 0) Django

This issue is caused by the failure of Pipenv 2018.10.9. To resolve this issue, use Pipenv 2018.5.18. For more, read here.

Are you facing this error in Flask?

Many times, when you receive the data from HTTP requests, they are fetched as bytes. So, if you face JSONDecodeError, you might be dealing with a JSON in bytes. First, you need to decode the JSON by using response.decode(‘utf-8’) . This will create a string, and then you can parse it.

json.loads() method can be used to parse JSON in python. For instance:
import json
json_string = ‘<«a»:»1″, «b»:»2″, «c»:»3″>‘
json_to_dict = json.loads(json_string)
print(json_to_dict)
print(type(json_to_dict))

import os
file_path = «/home/nikhilomkar/Desktop/test.json»
print(«File is empty!» if os.stat(file_path).st_size == 0 else «File isn’t empty!» )
The following code checks if the file is empty and prints the File is empty! If true, else, the File isn’t empty!.

Conclusion JSONDecodeError: Expecting value: line 1 column 1 (char 0)

The following article discussed the JSONDecodeError: Expecting value: line 1 column 1 (char 0). This error is due to various decoding and formatting errors. We looked at likely situations where it can occur and how to resolve it.

Источник

Cope with JSONDecodeError in requests.get().json() in Python 2 and 3

16 November 2016 6 comment s Python

This blog post is 6 years old! Most likely, its content is outdated. Especially if it’s technical.

UPDATE June 2020

Thanks to commentors, I’ve been made aware that requests will use simplejson if it’s installed to handle the deserialization of the JSON. So if you have simplejson in your requirements.txt or pyproject.toml you have to change to this:

Or, make your app more solid by doing the same trick that requests does:

Now, back to the old blog post.

Suppose you don’t know with a hundred percent certainty that an API will respond in with a JSON payload you need to protect yourself.

This is how you do it in Python 3:

This is how you do it in Python 2:

Here’s how you make the code work across both:

My immediate thought was why not do something like this, but then I thought for the sake of repetition and clarity your solution makes more sense.

import json
import requests

response = requests.get(url)
try:
print(response.json())
except (json.decoder.JSONDecodeError, ValueError):
print(«N’est pas JSON»)

JSONDecodeError is a subclass of ValueError, so I’d expect a simple `except ValueError:` to work fine on both versions.

Cool! I didn’t know that. But it makes perfect sense.
Granted there are benefits to using specific exceptions to avoid «over swallowing» errors if you cover multiple lines.

simplejson.scanner.JSONDecodeError in py2

The exception is wrong in this guide. requests uses simplejson not json, so the exception will not be caught.

.local/lib/python3.6/site-packages/requests/models.py», line 898, in json
return complexjson.loads(self.text, **kwargs)
File «/usr/lib/python3/dist-packages/simplejson/__init__.py», line 518, in loads
return _default_decoder.decode(s)
File «/usr/lib/python3/dist-packages/simplejson/decoder.py», line 370, in decode
obj, end = self.raw_decode(s)
File «/usr/lib/python3/dist-packages/simplejson/decoder.py», line 400, in raw_decode
return self.scan_once(s, idx=_w(s, idx).end())
simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

It depends on if you have `simplejson` installed or not. If you look at `site-packages/requests/compat.py` you’ll see that it does this:

try:
import simplejson as json
except ImportError:
import json

Basically, if `simplejson` is installed in the environment, requests uses that instead.

You need to be aware of that when you are aware of `response.json()` potentially failing.

I think I need to write an update about that.

Источник

Python JSONDecodeError: Expecting value: line 1 column 1 (char 0)

If you try to parse invalid JSON or decode an empty string as JSON, you will encounter the JSONDecodeError: Expecting value: line 1 column 1 (char 0). This error can occur if you read an empty file using json.load, read an empty JSON or receive an empty response from an API call.

You can use a try-except code block to catch the error and then check the contents of the JSON string or file before retrying.

This tutorial will go through the error in detail and how to solve it with code examples.

Table of contents

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

In Python, JSONDecodeError occurs when there is an issue with the formatting of the JSON data. This specific error tells us the JSON decoder has encountered an empty JSON.

Example #1: Incorrect use of json.loads()

Let’s look at an example where we have a JSON file with the following contents:

We want to read the data into a program using the json library. Let’s look at the code:

In the above code, we try to read the data in using json.loads() . Let’s run the code to see the result:

The error occurs because json.loads() expects a JSON encoded string, not a filename. The string pizza.json is not a valid JSON encoded string.

Solution

We need to use json.load() instead of json.loads() to read a file. Let’s look at the revised code:

In the above code, we use the open() function to create a file object that json.load() can read and return the decoded data object. The with statement is a context manager that ensures that the file is closed once the code is complete. Let’s run the code to see the result:

Example #2: Empty JSON file

Let’s look at an example where we have an empty file, which we will try to read in using json.loads() . The file is called particles.json . Since the JSON file is empty, the JSON decoder will throw the JSONDecodeError when it tries to read the file’s contents. Let’s look at the code:

Solution

If the file is empty, it is good practice to add a try-except statement to catch the JSONDecodeError. Let’s look at the code:

Now we have verified the file is empty, we can add some content to the file. We will add three particle names together with their masses.

Let’s try to read the JSON file into our program and print the contents to the console:

We successfully read the contents of the file into a list object.

Example #3: Response Request

Let’s look at an example where we want to parse a JSON response using the requests library. We will send a RESTful GET call to a server, and in return, we get a response in JSON format. The requests library has a built-in JSON decoder, response.json(), which provides the payload data in the JSON serialized format.

We may encounter a response that has an error status code or is not content-type application/json . We must check that the response status code is 200 (OK) before performing the JSON parse. Let’s look at the code to check the response has the 200 status code and has the valid content type. application/json .

In the above example, we use httpbin.org to execute a GET call. Let’s run the code to get the result:

Summary

Congratulations on reading to the end of this tutorial! The JSONDecodeError: Expecting value: line 1 column 1 (char 0) occurs either when data is not present in a file, the JSON string is empty, or the payload from a RESTful call is empty.

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 .

For further reading on errors involving JSON, go to the articles:

Источник

Quickstart¶

Eager to get started? This page gives a good introduction in how to get started with Requests.

First, make sure that:

Let’s get started with some simple examples.

Make a Request¶

Making a request with Requests is very simple.

Begin by importing the Requests module:

Now, let’s try to get a webpage. For this example, let’s get GitHub’s public timeline:

Now, we have a Response object called r . We can get all the information we need from this object.

Requests’ simple API means that all forms of HTTP request are as obvious. For example, this is how you make an HTTP POST request:

Nice, right? What about the other HTTP request types: PUT, DELETE, HEAD and OPTIONS? These are all just as simple:

That’s all well and good, but it’s also only the start of what Requests can do.

Passing Parameters In URLs¶

You often want to send some sort of data in the URL’s query string. If you were constructing the URL by hand, this data would be given as key/value pairs in the URL after a question mark, e.g. httpbin.org/get?key=val . Requests allows you to provide these arguments as a dictionary of strings, using the params keyword argument. As an example, if you wanted to pass key1=value1 and key2=value2 to httpbin.org/get , you would use the following code:

You can see that the URL has been correctly encoded by printing the URL:

Note that any dictionary key whose value is None will not be added to the URL’s query string.

You can also pass a list of items as a value:

Response Content¶

We can read the content of the server’s response. Consider the GitHub timeline again:

Requests will automatically decode content from the server. Most unicode charsets are seamlessly decoded.

When you make a request, Requests makes educated guesses about the encoding of the response based on the HTTP headers. The text encoding guessed by Requests is used when you access r.text . You can find out what encoding Requests is using, and change it, using the r.encoding property:

If you change the encoding, Requests will use the new value of r.encoding whenever you call r.text . You might want to do this in any situation where you can apply special logic to work out what the encoding of the content will be. For example, HTML and XML have the ability to specify their encoding in their body. In situations like this, you should use r.content to find the encoding, and then set r.encoding . This will let you use r.text with the correct encoding.

Requests will also use custom encodings in the event that you need them. If you have created your own encoding and registered it with the codecs module, you can simply use the codec name as the value of r.encoding and Requests will handle the decoding for you.

Binary Response Content¶

You can also access the response body as bytes, for non-text requests:

The gzip and deflate transfer-encodings are automatically decoded for you.

The br transfer-encoding is automatically decoded for you if a Brotli library like brotli or brotlicffi is installed.

For example, to create an image from binary data returned by a request, you can use the following code:

JSON Response Content¶

There’s also a builtin JSON decoder, in case you’re dealing with JSON data:

In case the JSON decoding fails, r.json() raises an exception. For example, if the response gets a 204 (No Content), or if the response contains invalid JSON, attempting r.json() raises requests.exceptions.JSONDecodeError . This wrapper exception provides interoperability for multiple exceptions that may be thrown by different python versions and json serialization libraries.

It should be noted that the success of the call to r.json() does not indicate the success of the response. Some servers may return a JSON object in a failed response (e.g. error details with HTTP 500). Such JSON will be decoded and returned. To check that a request is successful, use r.raise_for_status() or check r.status_code is what you expect.

Raw Response Content¶

In the rare case that you’d like to get the raw socket response from the server, you can access r.raw . If you want to do this, make sure you set stream=True in your initial request. Once you do, you can do this:

In general, however, you should use a pattern like this to save what is being streamed to a file:

Using Response.iter_content will handle a lot of what you would otherwise have to handle when using Response.raw directly. When streaming a download, the above is the preferred and recommended way to retrieve the content. Note that chunk_size can be freely adjusted to a number that may better fit your use cases.

An important note about using Response.iter_content versus Response.raw . Response.iter_content will automatically decode the gzip and deflate transfer-encodings. Response.raw is a raw stream of bytes – it does not transform the response content. If you really need access to the bytes as they were returned, use Response.raw .

If you’d like to add HTTP headers to a request, simply pass in a dict to the headers parameter.

For example, we didn’t specify our user-agent in the previous example:

Note: Custom headers are given less precedence than more specific sources of information. For instance:

Authorization headers set with headers= will be overridden if credentials are specified in .netrc , which in turn will be overridden by the auth= parameter. Requests will search for the netrc file at

/_netrc , or at the path specified by the NETRC environment variable.

Authorization headers will be removed if you get redirected off-host.

Proxy-Authorization headers will be overridden by proxy credentials provided in the URL.

Content-Length headers will be overridden when we can determine the length of the content.

Furthermore, Requests does not change its behavior at all based on which custom headers are specified. The headers are simply passed on into the final request.

Note: All header values must be a string , bytestring, or unicode. While permitted, it’s advised to avoid passing unicode header values.

More complicated POST requests¶

Typically, you want to send some form-encoded data — much like an HTML form. To do this, simply pass a dictionary to the data argument. Your dictionary of data will automatically be form-encoded when the request is made:

The data argument can also have multiple values for each key. This can be done by making data either a list of tuples or a dictionary with lists as values. This is particularly useful when the form has multiple elements that use the same key:

There are times that you may want to send data that is not form-encoded. If you pass in a string instead of a dict , that data will be posted directly.

For example, the GitHub API v3 accepts JSON-Encoded POST/PATCH data:

Please note that the above code will NOT add the Content-Type header (so in particular it will NOT set it to application/json ).

If you need that header set and you don’t want to encode the dict yourself, you can also pass it directly using the json parameter (added in version 2.4.2) and it will be encoded automatically:

Note, the json parameter is ignored if either data or files is passed.

POST a Multipart-Encoded File¶

Requests makes it simple to upload Multipart-encoded files:

You can set the filename, content_type and headers explicitly:

If you want, you can send strings to be received as files:

In the event you are posting a very large file as a multipart/form-data request, you may want to stream the request. By default, requests does not support this, but there is a separate package which does — requests-toolbelt . You should read the toolbelt’s documentation for more details about how to use it.

For sending multiple files in one request refer to the advanced section.

It is strongly recommended that you open files in binary mode . This is because Requests may attempt to provide the Content-Length header for you, and if it does this value will be set to the number of bytes in the file. Errors may occur if you open the file in text mode.

Response Status Codes¶

We can check the response status code:

Requests also comes with a built-in status code lookup object for easy reference:

If we made a bad request (a 4XX client error or 5XX server error response), we can raise it with Response.raise_for_status() :

But, since our status_code for r was 200 , when we call raise_for_status() we get:

We can view the server’s response headers using a Python dictionary:

The dictionary is special, though: it’s made just for HTTP headers. According to RFC 7230, HTTP Header names are case-insensitive.

So, we can access the headers using any capitalization we want:

It is also special in that the server could have sent the same header multiple times with different values, but requests combines them so they can be represented in the dictionary within a single mapping, as per RFC 7230:

A recipient MAY combine multiple header fields with the same field name into one “field-name: field-value” pair, without changing the semantics of the message, by appending each subsequent field value to the combined field value in order, separated by a comma.

Cookies¶

If a response contains some Cookies, you can quickly access them:

To send your own cookies to the server, you can use the cookies parameter:

Cookies are returned in a RequestsCookieJar , which acts like a dict but also offers a more complete interface, suitable for use over multiple domains or paths. Cookie jars can also be passed in to requests:

Redirection and History¶

By default Requests will perform location redirection for all verbs except HEAD.

We can use the history property of the Response object to track redirection.

The Response.history list contains the Response objects that were created in order to complete the request. The list is sorted from the oldest to the most recent response.

For example, GitHub redirects all HTTP requests to HTTPS:

If you’re using GET, OPTIONS, POST, PUT, PATCH or DELETE, you can disable redirection handling with the allow_redirects parameter:

If you’re using HEAD, you can enable redirection as well:

Timeouts¶

You can tell Requests to stop waiting for a response after a given number of seconds with the timeout parameter. Nearly all production code should use this parameter in nearly all requests. Failure to do so can cause your program to hang indefinitely:

timeout is not a time limit on the entire response download; rather, an exception is raised if the server has not issued a response for timeout seconds (more precisely, if no bytes have been received on the underlying socket for timeout seconds). If no timeout is specified explicitly, requests do not time out.

Errors and Exceptions¶

In the event of a network problem (e.g. DNS failure, refused connection, etc), Requests will raise a ConnectionError exception.

Response.raise_for_status() will raise an HTTPError if the HTTP request returned an unsuccessful status code.

If a request times out, a Timeout exception is raised.

If a request exceeds the configured number of maximum redirections, a TooManyRedirects exception is raised.

All exceptions that Requests explicitly raises inherit from requests.exceptions.RequestException .

Ready for more? Check out the advanced section.

If you’re on the job market, consider taking this programming quiz. A substantial donation will be made to this project, if you find a job through this platform.

Requests is an elegant and simple HTTP library for Python, built for human beings. You are currently looking at the documentation of the development release.

Источник

If you try to parse invalid JSON or decode an empty string as JSON, you will encounter the JSONDecodeError: Expecting value: line 1 column 1 (char 0). This error can occur if you read an empty file using json.load, read an empty JSON or receive an empty response from an API call.

You can use a try-except code block to catch the error and then check the contents of the JSON string or file before retrying.

This tutorial will go through the error in detail and how to solve it with code examples.


Table of contents

  • JSONDecodeError: Expecting value: line 1 column 1 (char 0)
  • Example #1: Incorrect use of json.loads()
    • Solution
  • Example #2: Empty JSON file
    • Solution
  • Example #3: Response Request
  • Summary

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

In Python, JSONDecodeError occurs when there is an issue with the formatting of the JSON data. This specific error tells us the JSON decoder has encountered an empty JSON.

Example #1: Incorrect use of json.loads()

Let’s look at an example where we have a JSON file with the following contents:

[
        {"margherita":7.99},
        {"pepperoni":9.99},
        {"four cheeses":10.99}
]

We want to read the data into a program using the json library. Let’s look at the code:

import json

json_path = 'pizza.json'

data = json.loads(json_path)

In the above code, we try to read the data in using json.loads(). Let’s run the code to see the result:

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

The error occurs because json.loads() expects a JSON encoded string, not a filename. The string pizza.json is not a valid JSON encoded string.

Solution

We need to use json.load() instead of json.loads() to read a file. Let’s look at the revised code:

import json

json_path = 'pizza.json'

with open(json_path, 'r') as f:

    data = json.loads(f.read())

print(data)

In the above code, we use the open() function to create a file object that json.load() can read and return the decoded data object. The with statement is a context manager that ensures that the file is closed once the code is complete. Let’s run the code to see the result:

[{'margherita': 7.99}, {'pepperoni': 9.99}, {'four cheeses': 10.99}]

Example #2: Empty JSON file

Let’s look at an example where we have an empty file, which we will try to read in using json.loads(). The file is called particles.json. Since the JSON file is empty, the JSON decoder will throw the JSONDecodeError when it tries to read the file’s contents. Let’s look at the code:

import json

filename = 'particles.json'

with open(filename, 'r') as f:
    contents = json.loads(f.read())
    print(contents)
---------------------------------------------------------------------------
JSONDecodeError                           Traceback (most recent call last)
Input In [1], in <cell line: 5>()
      3 filename = 'particles.json'
      5 with open(filename, 'r') as f:
----> 6     contents = json.loads(f.read())
      7     print(contents)

File ~/opt/anaconda3/lib/python3.8/json/__init__.py:357, in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    352     del kw['encoding']
    354 if (cls is None and object_hook is None and
    355         parse_int is None and parse_float is None and
    356         parse_constant is None and object_pairs_hook is None and not kw):
--> 357     return _default_decoder.decode(s)
    358 if cls is None:
    359     cls = JSONDecoder

File ~/opt/anaconda3/lib/python3.8/json/decoder.py:337, in JSONDecoder.decode(self, s, _w)
    332 def decode(self, s, _w=WHITESPACE.match):
    333     """Return the Python representation of ``s`` (a ``str`` instance
    334     containing a JSON document).
    335 
    336     """
--> 337     obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    338     end = _w(s, end).end()
    339     if end != len(s):

File ~/opt/anaconda3/lib/python3.8/json/decoder.py:355, in JSONDecoder.raw_decode(self, s, idx)
    353     obj, end = self.scan_once(s, idx)
    354 except StopIteration as err:
--> 355     raise JSONDecodeError("Expecting value", s, err.value) from None
    356 return obj, end

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Solution

If the file is empty, it is good practice to add a try-except statement to catch the JSONDecodeError. Let’s look at the code:

import json

filename = 'particles.json'

with open(filename, 'r') as f:

   try:

       contents = json.loads(f.read())

       print(contents)

   except json.decoder.JSONDecodeError:

       print('File is empty')
File is empty

Now we have verified the file is empty, we can add some content to the file. We will add three particle names together with their masses.

[
        {"proton":938.3},
        {"neutron":939.6},
        {"electron":0.51}
]

Let’s try to read the JSON file into our program and print the contents to the console:

import json

filename = 'particles.json'

with open(filename, 'r') as f:

   try:

       contents = json.loads(f.read())

       print(contents)

   except json.decoder.JSONDecodeError:

       print('File is empty')
[{'proton': 938.3}, {'neutron': 939.6}, {'electron': 0.51}]

We successfully read the contents of the file into a list object.

Example #3: Response Request

Let’s look at an example where we want to parse a JSON response using the requests library. We will send a RESTful GET call to a server, and in return, we get a response in JSON format. The requests library has a built-in JSON decoder, response.json(), which provides the payload data in the JSON serialized format.

We may encounter a response that has an error status code or is not content-type application/json. We must check that the response status code is 200 (OK) before performing the JSON parse. Let’s look at the code to check the response has the 200 status code and has the valid content type. application/json.

import requests

from requests.exceptions import HTTPError

url = 'https://httpbin.org/get'

try:

    response = requests.get(url)

    status = response.status_code

    if (status != 204 and response.headers["content-type"].strip().startswith("application/json")):

        try:

            json_response = response.json()

            print(json_response)

        except ValueError:
            
            print('Bad Data from Server. Response content is not valid JSON')

    elif (status != 204):

        try:

            print(response.text)

        except ValueError:

            print('Bad Data From Server. Reponse content is not valid text')

except HTTPError as http_err:

    print(f'HTTP error occurred: {http_err}')

except Exception as err:

    print(f'Other error occurred: {err}')

In the above example, we use httpbin.org to execute a GET call. Let’s run the code to get the result:

{'args': {}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate, br', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.27.1', 'X-Amzn-Trace-Id': 'Root=1-6265a5c1-3b57327c02057a3a39ffe86d'}, 'origin': '90.206.95.191', 'url': 'https://httpbin.org/get'}

Summary

Congratulations on reading to the end of this tutorial! The JSONDecodeError: Expecting value: line 1 column 1 (char 0) occurs either when data is not present in a file, the JSON string is empty, or the payload from a RESTful call is empty.

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.

For further reading on errors involving JSON, go to the articles:

  • How to Solve Python ValueError: Trailing data
  • How to Solve Python JSONDecodeError: extra data
  • How to Solve Python TypeError: Object of type datetime is not JSON serializable
  • How to Solve Python JSONDecodeError: Expecting ‘,’ delimiter: line 1

Have fun and happy researching!

The error “JSONDecodeError: Expecting value: line 1 column 1 (char 0)” can happen when you working with Python. Learn why Python raises this exception and how you can resolve it.

Reproduce The Error

Python provides a built-in module for encoding and decoding JSON contents, which can be strings or files. The loads() is one of the most popular functions in this module. However, it raises a JSONDecodeError instead:

>>> import json

>>> json.loads(“”)

Traceback (most recent call last):

  File “/usr/lib/python3.10/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)

When you read an empty JSON file with load(), Python will also print out a similar error message:

>>> import json
>>> with open("empty.json", "r") as emptyJSON:
...     print(json.load(emptyJSON))
....
Traceback (most recent call last):
  File "/usr/lib/python3.10/json/decoder.py", line 355, in raw_decode
      raise JSONDecodeError("Expecting value", s, err.value) from None
    json.decoder.JSONDecodeError: Expecting value: line 2 column 1 (char 2)

You can run into this error outside the json module as well. Requests is a popular library for dealing with HTTP requests in Python. It supports the json() method, which converts the fetched content from a resource URI into a JSON object. But it can unexpectedly raise the JSONDecodeError exception:

>>> import requests
>>> URL="http://httpbin.org/status/200"
>>> response = requests.delete(URL)
>>> print(response.json())
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
…
    raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Explain The Error

The first two examples are obvious. The error message is displayed in those cases because you provide load() and loads() with empty JSON content.

When those methods fail to deserialize and parse a valid JSON document from those sources, they raise the JSONDecodeError exception.

From this clue, we can guess why the response.json() method in the third example also produces the same error.

There is a high chance the response object itself is empty. This is indeed true, which can be verified by using response.text to get the content of the response:

This is a common response message when we send an HTTP DELETE request to delete a resource. This can be the case even with the status code 200 (meaning the deleting action has been carried out).

Solutions

Refraining from parsing empty files and strings as JSON documents is an obvious fix. But in many situations, we can’t control whether a data source can be empty or not.

Python, however, allows us to handle exceptions like JSONDecodeError. For instance, you can use the try-catch statements below to prevent error messages in the case of empty strings and files:

Example 1

try:
	json.loads("")
except json.decoder.JSONDecodeError:
	print("")

Example 2

try:
    with open("empty.json", "r") as emptyJSON:
    	print(json.load(emptyJSON))
except json.decoder.JSONDecodeError:
    print("")

Those snippets execute the loads() and load() functions. If the JSONDecodeError exception is raised, Python executes the except clause and prints an empty string instead of an error message.

We can apply the same idea when parsing the content of an HTTP request response:

try:
    URL = "http://httpbin.org/status/200"
    response = requests.delete(URL)
    print(response.json())
except requests.exceptions.JSONDecodeError:
    print("")

It is important to note that you must change the exception to catch from json.decoder.JSONDecodeError to requests.exceptions.JSONDecodeError.

Summary

Python throws the error “JSONDecodeError: Expecting value: line 1 column 1 (char 0)” when it parses an empty string or file as JSON. You can use a try statement to catch and handle this exception.

Maybe you are interested in similar errors:

  • DataFrame constructor not properly called!
  • ModuleNotFoundError: No module named ‘tensorflow’ in python
  • ModuleNotFoundError: No module named ‘scipy’ in Python
  • RuntimeError: dictionary changed size during iteration

Robert J. Charles

My name is Robert. I have a degree in information technology and two years of expertise in software development. I’ve come to offer my understanding on programming languages. I hope you find my articles interesting.


Job: Developer
Name of the university: HUST
Major: IT
Programming Languages: Java, C#, C, Javascript, R, Typescript, ReactJs, Laravel, SQL, Python

Table of Contents
Hide
  1. JSONDecodeError: Expecting value: line 1 column 1 (char 0)
  2. Example JSONDecodeError: Expecting value: line 1 column 1 (char 0)
  3. Solution 
  4. Other Possible Solutions
    1. Ensure HTTP 200 status code and valid content type as application/json
    2. Invoke json.load() on the contents of the file

If you are working with APIs and trying to fetch and parse the JSON data while making the HTTP or curl requests and JSON response is not well-formed, Python will throw json.decoder.jsondecodeerror: expecting value: line 1 column 1 (char 0).

Let us take a look at the possible causes of JSONDecodeError and how we solve this issue in our code with some examples.

In most of cases, you get json.loads- JSONDecodeError: Expecting value: line 1 column 1 (char 0) error is due to :

  1. The response might be in some other format such as XML, HTML, etc.
  2. The JSON response is not structured properly.
  3. The response type doesn’t come in the format of application/json. Rather it comes in string format, which in turn throws a JSONDecodeError while parsing the response.
  4. Empty response 
  5. Invalid status code or httpx error

Also, read the Python JSONPath tutorial

Example JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Let me demonstrate with a simple example of replicating a JSONDecodeError, and we will look at solving the same. 

In this example, we are trying to load a JSON file from the specified path and print the contents of the JSON file. However, since the JSON file is empty, the JSON module will throw a JSONDecodeError when we try to read the empty content. It always expects the proper JSON structure.

import json

file_path = "C:/Projects/Tryouts/books.json"

with open(file_path, 'r') as j:
     contents = json.loads(j.read())
     print(contents)

Output

Traceback (most recent call last):
  File "c:/Projects/Tryouts/main.py", line 6, in <module>
    contents = json.loads(j.read())
  File "C:UsersabcAppDataLocalProgramsPythonPython37libjson__init__.py", line 348, in loads    
    return _default_decoder.decode(s)
  File "C:UsersabcAppDataLocalProgramsPythonPython37libjsondecoder.py", line 337, in decode    
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:UsersabcAppDataLocalProgramsPythonPython37libjsondecoder.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)

The json.loads(j.read()) tries to load the JSON content and parse the JSON, and during that time, if it’s not well structured, you get a JSONDecodeError.

Note: In this case, if the JSON file is not found, you will get a FileNotFoundError: [Errno 2] No such file or directory

Solution 

The solution is simple and straightforward. Validate if the file is not empty and has proper JSON content. Now that we have added the proper content to the books.json file when we run the same code again, we get the below output.

# Python program to solve JSONDecodeError: Expecting value: line 1 column 1 (char 0)
import json

file_path = "C:/Projects/Tryouts/books.json"

with open(file_path, 'r') as j:
     contents = json.loads(j.read())
     print(contents)

Output

{'category': 'reference', 'author': 'Nigel Rees', 'title': 'Sayings of the Century', 'isbn': '6-246-2356-8', 'price': 8.95}

Other Possible Solutions

Ensure HTTP 200 status code and valid content type as application/json

If you are making requests to API, make sure to check the response status code is 200 (OK) and then perform JSON parse. Below is the sample snippet on how you can ensure to check the response has 200 status code and valid content type as application/json

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

Invoke json.load() on the contents of the file

If you are calling and loading json files directly, ensure that you are not passing a file path directly to the json.load() method instead pass the contents into the json.load() method.

Bad Practice

json_file_path = "/path/to/example.json"

contents = json.loads(json_file_path)

Good Practice

json_file_path = "/path/to/example.json"

with open(json_file_path, 'r') as j:
     contents = json.loads(j.read())

Avatar Of Srinivas Ramakrishna

Srinivas Ramakrishna is a Solution Architect and has 14+ Years of Experience in the Software Industry. He has published many articles on Medium, Hackernoon, dev.to and solved many problems in StackOverflow. He has core expertise in various technologies such as Microsoft .NET Core, Python, Node.JS, JavaScript, Cloud (Azure), RDBMS (MSSQL), React, Powershell, etc.

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

By checking this box, you confirm that you have read and are agreeing to our terms of use regarding the storage of the data submitted through this form.

Понравилась статья? Поделить с друзьями:

Читайте также:

  • Json api error response
  • Jscript9 dll ошибка как исправить windows 7
  • Js обработка ошибок fetch
  • Js как изменить текст тега
  • Js как изменить стиль у класса

  • 0 0 голоса
    Рейтинг статьи
    Подписаться
    Уведомить о
    guest

    0 комментариев
    Старые
    Новые Популярные
    Межтекстовые Отзывы
    Посмотреть все комментарии