TATECHATLAS
◎ English
Programming / Guide

Python: Reading JSON from Strings and Files, and Handling Decoding Errors

Learn how to parse JSON data in Python from both string literals and files, and how to effectively handle potential decoding errors using the built-in json module.

On this page

Python's json module provides json.loads() to parse JSON from strings and json.load() to parse from file-like objects. Both functions can raise json.JSONDecodeError if the input is not valid JSON, which can be caught using a try-except block to handle malformed data gracefully.

Reading JSON from a String

The json.loads() function is used to deserialize a JSON string into a Python object. It takes a string as input and returns the corresponding Python data structure (e.g., dictionaries, lists, strings, numbers, booleans, None). This is useful when you have JSON data embedded directly within your Python code or received as a string from an API response.

For example, to parse a simple JSON object represented as a string, you would pass the string to json.loads().

import json

json_string = '{"name": "Alice", "age": 30, "isStudent": false}'

try:
    data = json.loads(json_string)
    print(data)
    print(f"Name: {data['name']}, Age: {data['age']}")
except json.JSONDecodeError as e:
    print(f"Error decoding JSON string: {e}")

Reading JSON from a File

When your JSON data is stored in a file, you should use the json.load() function. This function takes a file-like object (opened in text mode) as input and reads the JSON data from it, deserializing it into a Python object. It's crucial to open the file correctly, ensuring it's read as text.

The with open(...) statement is the recommended way to handle files, as it ensures the file is properly closed even if errors occur. The json.load() function then processes the file's content.

import json

# Assume 'data.json' contains: [{"id": 1, "value": "A"}, {"id": 2, "value": "B"}]
file_path = 'data.json'

try:
    with open(file_path, 'r', encoding='utf-8') as f:
        data = json.load(f)
        print(data)
except FileNotFoundError:
    print(f"Error: File not found at {file_path}")
except json.JSONDecodeError as e:
    print(f"Error decoding JSON from file: {e}")

Handling JSON Decoding Errors

JSON data must adhere to a strict format. If the input string or file content is not valid JSON, Python's json module will raise a json.JSONDecodeError. This exception provides details about where the parsing failed, such as the position and the nature of the error.

To handle these errors gracefully, you should wrap your json.loads() or json.load() calls within a try...except json.JSONDecodeError block. This allows your program to continue running or to provide informative messages to the user instead of crashing.

import json

malformed_json_string = '{"key": "value", "another_key": }' # Missing value

try:
    json.loads(malformed_json_string)
except json.JSONDecodeError as e:
    print(f"Caught a JSONDecodeError: {e}")
    print(f"Error occurred at line {e.lineno}, column {e.colno}")
    print(f"Error message: {e.msg}")

Understanding JSONDecodeError Details

The json.JSONDecodeError exception object contains useful attributes that can help pinpoint the exact issue in your JSON data. These include msg (the error message), doc (the input string being parsed), pos (the character index in the document where the error occurred), lineno (the line number), and colno (the column number).

By inspecting these attributes, you can provide more specific feedback about the malformed JSON, aiding in debugging or informing the user about the data quality issue.

import json

json_string_with_error = '''
{
    "name": "Bob",
    "details": {
        "city": "New York",
        "zip": 10001
        "country": "USA"
    }
}
'''

try:
    json.loads(json_string_with_error)
except json.JSONDecodeError as e:
    print(f"Error: {e.msg}")
    print(f"Document: {e.doc[max(0, e.pos-10):e.pos+10]}...") # Show context
    print(f"Position: {e.pos}, Line: {e.lineno}, Column: {e.colno}")

Customizing JSON Decoding

The json.load() and json.loads() functions offer parameters like object_hook, parse_float, parse_int, and parse_constant to customize how JSON data is decoded. For instance, parse_float can be used to deserialize floating-point numbers into Decimal objects instead of standard floats, which is useful for financial applications requiring precise decimal arithmetic.

Similarly, object_hook can transform decoded JSON objects (Python dictionaries) into custom class instances, enabling more object-oriented data handling.

parse_float applies only to numeric JSON tokens containing a decimal point, not to quoted strings. Here price is the number 19.99 and becomes Decimal("19.99"); the illustrative type output is Decimal. A value written as "19.99" remains a string and needs an explicit, validated conversion. object_hook applies to decoded objects, not to individual numeric tokens.

import json
import decimal

json_string_with_floats = '{"price": 19.99, "tax": 1.50}'

# Use Decimal for precise float parsing
data = json.loads(json_string_with_floats, parse_float=decimal.Decimal)

print(f"Parsed data: {data}")
print(type(data['price']).__name__)

# Example with object_hook to create a custom object
def dict_to_person(d):
    if '__class__' in d and d['__class__'] == 'Person':
        return Person(d['name'], d['age'])
    return d

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age
    def __repr__(self):
        return f"Person(name='{self.name}', age={self.age})"

json_person = '{"__class__": "Person", "name": "Charlie", "age": 25}'
person_obj = json.loads(json_person, object_hook=dict_to_person)
print(f"Decoded object: {person_obj}")

Encoding Python Objects to JSON

While the focus is on reading JSON, it's worth noting the inverse operation: encoding Python objects into JSON strings or files using json.dumps() and json.dump(), respectively. These functions handle the conversion of Python data types to their JSON equivalents.

They also support customization, such as default for handling non-serializable types, indent for pretty-printing, and sort_keys for ordered output, which can be very helpful for debugging and readability.

import json

python_data = {
    "name": "David",
    "scores": [95, 88, 76],
    "isActive": True,
    "metadata": None
}

# Encode to a JSON string with pretty-printing
json_output_string = json.dumps(python_data, indent=4, sort_keys=True)
print("--- JSON String Output ---")
print(json_output_string)

# Encode to a file (example using StringIO for demonstration)
from io import StringIO
output_file = StringIO()
json.dump(python_data, output_file, indent=4)
output_file.seek(0) # Rewind to read the content
print("\n--- JSON File Output (simulated) ---")
print(output_file.read())

Security Considerations

Limit the bytes accepted from the input stream before parsing. A length check on an already loaded string does not prevent memory use during loading, and a byte limit alone does not bound CPU time or nesting depth. The standard decoder loads a complete document; use separate execution/resource limits or a suitable streaming parser when the input risk requires them.

check_circular belongs to json.dump and json.dumps, which encode Python objects. It is not a parameter of json.load or json.loads. The separate illustration below creates a Python dictionary referring to itself: with the check enabled, encoding raises ValueError for a circular reference. Disabling the check can lead to RecursionError or worse; it is not a repair or a defense for incoming JSON text.

import json

max_bytes = 1024 * 1024
raw = b'{"key": "value"}'
if len(raw) > max_bytes:
    raise ValueError("Input exceeds the byte limit")
data = json.loads(raw)
print(data)

cyclic = {}
cyclic["self"] = cyclic
try:
    json.dumps(cyclic, check_circular=True)
except ValueError:
    print("Circular reference detected during encoding")

Using pathlib for File Paths

When working with JSON files, managing file paths is often necessary. Python's pathlib module offers an object-oriented approach to filesystem paths, making it cleaner and more robust than using string manipulation with os.path.

You can create Path objects and use them directly with open() and json.load(). This integrates file handling and JSON parsing seamlessly. pathlib handles path separators correctly across different operating systems.

import json
from pathlib import Path

# Define the path to the JSON file using pathlib
file_path = Path('config.json')

# Ensure the file exists and contains valid JSON, e.g.:
# {"database": "localhost", "port": 5432}

try:
    # Use the Path object directly with open()
    with file_path.open('r', encoding='utf-8') as f:
        config_data = json.load(f)
        print(f"Configuration loaded: {config_data}")
        print(f"Database host: {config_data.get('database')}")
except FileNotFoundError:
    print(f"Error: Configuration file not found at {file_path}")
except json.JSONDecodeError as e:
    print(f"Error decoding JSON from {file_path}: {e}")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

Things to check

  • Verify that the JSON string or file content is correctly formatted according to JSON specifications (e.g., keys and strings in double quotes, correct use of commas, braces, and brackets).
  • Ensure that file paths used with json.load() are correct and that the file has the appropriate read permissions.
  • Implement try-except blocks around json.loads() and json.load() to catch json.JSONDecodeError and handle malformed JSON gracefully.
  • Consider using encoding='utf-8' when opening files for reading JSON to avoid potential encoding issues.
  • For untrusted data, implement size limits on the input JSON to mitigate denial-of-service risks.

The json module decodes JSON into standard Python types. For complex custom types or specific numerical precision (like financial calculations), you might need to use object_hook or parse_float with custom functions (e.g., decimal.Decimal). The module parses the entire JSON document into memory, which can be an issue for extremely large files; alternative streaming JSON parsers might be needed in such cases. Security warnings regarding untrusted sources are important; always validate or sanitize input where possible.

Sources

  1. Python: json ↗
  2. Python: pathlib ↗
Back to top ↑