Categories
Python Answers

How to fix JSONDecodeError: Expecting value: line 1 column 1 (char 0) with Python?

Spread the love

Sometimes, we want to fix JSONDecodeError: Expecting value: line 1 column 1 (char 0) with Python.

In this article, we’ll look at how to fix JSONDecodeError: Expecting value: line 1 column 1 (char 0) with Python.

How to fix JSONDecodeError: Expecting value: line 1 column 1 (char 0) with Python?

To fix JSONDecodeError: Expecting value: line 1 column 1 (char 0) with Python, we can put the code that parse the JSON string in the try block.

For instance, we write

import requests

# ...
def make_request(url):
    response = requests.get(url)

    try:
        return response.json()
    except ValueError:
        # ...

to make a GET request with requests.get to url.

Then we put the response.json call in the try block to catch the ValueError that’s raised if response has invalid JSON.

Conclusion

To fix JSONDecodeError: Expecting value: line 1 column 1 (char 0) with Python, we can put the code that parse the JSON string in the try block.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *