pysimdjson

7.0.2 · active · verified Mon Apr 13

pysimdjson provides high-performance Python bindings for the simdjson C++ library, a SIMD-accelerated JSON parser. It offers both a compatibility API similar to Python's built-in `json` module and a native API for significantly faster parsing, especially when only parts of a JSON document are needed. The library is actively maintained, with the current version being 7.0.2.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates both the high-performance native API using `pysimdjson.Parser` for selective data extraction and the `pysimdjson.loads` function for full document parsing, similar to the standard `json` module. The native API is generally preferred for large documents to avoid unnecessary object materialization.

from pysimdjson import Parser

json_data = b'{"name": "Alice", "age": 30, "city": "New York", "details": {"occupation": "Engineer", "hobbies": ["reading", "hiking"]}}'

# Using the native Parser API for performance and partial loading
parser = Parser()
try:
    # Parsing bytes is generally fastest
    doc = parser.parse(json_data)

    # Accessing elements without fully materializing the document
    name = doc['name'].as_str()
    age = doc['age'].as_int()
    occupation = doc['details']['occupation'].as_str()
    first_hobby = doc['details']['hobbies'][0].as_str()

    print(f"Name: {name}, Age: {age}")
    print(f"Occupation: {occupation}, First Hobby: {first_hobby}")

    # Convert a subtree to a Python object if needed
    details_dict = doc['details'].as_dict()
    print(f"Details as dict: {details_dict}")

except RuntimeError as e:
    print(f"Error during parsing or access: {e}")

# For simple full document loading, compatible with json.loads
from pysimdjson import loads
full_python_obj = loads(json_data)
print(f"Full Python object (loads): {full_python_obj}")

view raw JSON →