ijson

3.5.0 · active · verified Sat Mar 28

Ijson is an iterative JSON parser for Python that provides standard iterator interfaces. It enables efficient processing of large JSON data streams without loading the entire document into memory, making it ideal for handling massive JSON files, streaming APIs, and memory-constrained environments. The library is currently at version 3.5.0 and maintains an active release cadence with regular updates and binary wheel support for major platforms.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `ijson.items` to iteratively parse JSON data, extracting Python objects from specified paths. It also shows how to explicitly select a backend for improved performance. `ijson` expects file-like objects opened in binary mode (`'rb'`). The path syntax uses `.` for object keys and `.item` for elements within arrays.

import ijson
import os
from io import BytesIO

# Example JSON data (simulating a file-like object)
json_data = b'{"earth": {"europe": [{"name": "Paris", "type": "city"}, {"name": "Rome", "type": "city"}]}, "america": [{"name": "New York", "type": "city"}]}'

# For demonstration, you might use BytesIO or a real file opened in binary mode
with BytesIO(json_data) as f:
    # Using the 'items' function to extract objects under a specific path
    # 'earth.europe.item' means: 'earth' object, then 'europe' array, then each 'item' in the array
    print("European cities:")
    for city in ijson.items(f, 'earth.europe.item'):
        print(city)

# Reset stream for another parse, or open a new file
with BytesIO(json_data) as f:
    print("\nAll cities:")
    # Using 'item' for a top-level array or '.item' for nested array items without specific object keys
    # Or a more general path if structure is less strict
    for city_or_state in ijson.items(f, 'earth..item'): # Matches any item within 'earth' object (e.g., europe.item, america.item)
        if isinstance(city_or_state, dict) and city_or_state.get('type') == 'city':
            print(city_or_state)

# Example with explicit backend selection (recommended for production)
# Ensure 'ijson[yajl2_cffi]' is installed for this to be effective
try:
    import ijson.backends.yajl2_cffi as ijson_fast
    with BytesIO(json_data) as f:
        print("\nEuropean cities (with yajl2_cffi backend):")
        for city in ijson_fast.items(f, 'earth.europe.item'):
            print(city)
except ImportError:
    print("\n'yajl2_cffi' backend not available. Install with 'pip install ijson[yajl2_cffi]'.")

view raw JSON →