Open In App

orjson.JSONDecodeError in Python

Last Updated : 03 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

When working with JSON data in Python, errors may occur during decoding, especially if the data is malformed or incorrectly formatted. The orjson library, known for its speed and efficiency in handling JSON, provides a specific exception called orjson.JSONDecodeError to handle such situations. In this article, we’ll explore what orjson.JSONDecodeError is, why it occurs, and how to handle it effectively with illustrative examples.

What is Python orjson.JSONDecodeError?

The orjson.JSONDecodeError is a specific exception raised by the orjson library when there’s an issue decoding JSON data. This error typically occurs when the JSON data being decoded is invalid or does not conform to the expected JSON format.

orjson.JSONDecodeError in Python

Below are the uses of orjson.JSONDecodeError in Python:

Handling Invalid JSON Data

In this example, we deliberately provide an incomplete JSON string, missing the closing bracket. As a result, orjson.loads() raises a JSONDecodeError, indicating the location and nature of the error.

Python3
import orjson

# Invalid JSON string
json_string = '{"name": "John", "age": 30, "city": "New York"'

try:
    # Parsing invalid JSON string
    data = orjson.loads(json_string)
    print(data)
except orjson.JSONDecodeError as e:
    print("Error parsing JSON:", e)

Output:

Error parsing JSON: unexpected end of data: line 1 column 47 (char 46)

Handling Unexpected JSON Structure

In this example, the JSON string has an unexpected structure, using square brackets ([]) instead of curly braces ({}). As a result, orjson.loads() raises a JSONDecodeError, indicating that it expects property names to be enclosed in double quotes.

Python3
import orjson

# Invalid JSON string with unexpected structure
json_string = '["name": "John", "age": 30, "city": "New York"]'

try:
    # Parsing invalid JSON string
    data = orjson.loads(json_string)
    print(data)
except orjson.JSONDecodeError as e:
    print("Error parsing JSON:", e)

Output:

Error parsing JSON: unexpected character: line 1 column 8 (char 7)

Handling Missing JSON Files

In this example, we attempt to read JSON data from a non-existent file. As expected, FileNotFoundError is raised, indicating that the file does not exist. However, if the file existed but contained invalid JSON data, orjson.JSONDecodeError would be raised instead.

a.json

'{"name": "John", "age": 30, "city": "New York"'
Python3
import orjson

# Non-existent JSON file path
json_file = 'a.json'

try:
    # Reading JSON data from file
    with open(json_file, 'r') as f:
        json_data = f.read()

    # Parsing JSON data using orjson.loads()
    data = orjson.loads(json_data)

    # Displaying the parsed data
    print(data)
except FileNotFoundError:
    print("File not found:", json_file)
except orjson.JSONDecodeError as e:
    print("Error parsing JSON:", e)

Output:

Error parsing JSON: unexpected character: line 1 column 1 (char 0)

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads