Skip to main content

ByteJSON

Random access to very large JSON files without loading the whole file into memory.

from bytejson import open

db = open("reddit.json")
print(db["posts"][500]["title"])
JSON file → binary index (byte offsets) → mmap → lazy reads → dict/list-like API

A hand-written scanner walks the file once and records where every value lives; its type, byte offset, length, and relationship to its parent; without materializing values into Python objects. The resulting metadata is written to a compact .bjidx index containing an mmap-backed node table, dictionary-encoded keys, and a CSR-style children index. Object lookups use indexed keys, while array lookups resolve directly by index without scanning.

After the index is built, db["a"]["b"][3] jumps directly to the relevant bytes and decodes only the requested value.

Why ByteJSON?

Python's built-in json.load() is a great choice when you need most or all of a JSON document. For very large JSON files, however, loading the entire document can require substantial memory even when an application only needs a small number of values.

ByteJSON targets the opposite workload: large, mostly read-only JSON documents where applications perform a relatively small number of random lookups.

The trade-off is an index-building step and additional disk space for the .bjidx index.

Setup

uv sync
uv run python examples/generate_sample.py
uv run python examples/demo.py
uv run python examples/benchmark.py
uv run pytest

Performance

Measured with examples/benchmark.py using approximately 15,000 posts and 5,000 users (~8 MB JSON). Run the benchmark on your own data for representative numbers.

Metric Result
Index size ~123–128% of source JSON
Warm open (existing index) ~1–5 ms
Random-access crossover vs json.load() roughly 10,000–20,000+ lookups
Peak Python-heap memory (warm) single-digit MB

Below the crossover point, ByteJSON can benefit workloads that perform relatively few random lookups because it avoids materializing the entire document. As the number of lookups increases, the overhead of repeatedly resolving and decoding values eventually outweighs the benefit of lazy access, and a fully parsed Python object can become faster.

The crossover point is workload- and data-dependent. See examples/benchmark.py for the complete benchmark output and methodology.

Example

from bytejson import open

db = open("reddit.json")

posts = db["posts"]

post = posts[500]

title = posts[500]["title"]

print(title)

Objects and arrays behave like lightweight dictionary/list-like views rather than ordinary materialized Python objects.

Project structure

bytejson/
    __init__.py       public API: build_index, open, ByteJSONFile
    index.py          scanner: walks the file and locates every value
    storage.py        binary index: node table, key dictionary,
                      and CSR children index
    parser.py         decodes a located byte range into a Python value
    lazy.py           LazyObject / LazyArray dict/list-like views
    mmap_reader.py    mmap-backed random-access file reader
    utils.py          byte-class constants and file fingerprinting

examples/
    generate_sample.py
    demo.py
    benchmark.py

tests/
    pytest suite

Scope

Currently supported:

  • Objects
  • Arrays
  • Strings
  • Numbers
  • Booleans
  • null
  • Random access through dictionary/list-like APIs

Currently out of scope:

  • JSONPath queries
  • Writes or mutation
  • Compression
  • Streaming index construction

Known limitations

  • Index construction is currently single-threaded. Building the index requires scanning the entire JSON document and is O(document size). For cold opens, index construction is the dominant cost.

  • Deeply nested JSON. The scanner recurses for nested structures. Extremely deep JSON documents, potentially thousands of levels, could hit Python's recursion limit.

  • File-change detection is partial. ByteJSON currently samples the first and last 4 KiB of the JSON file along with its size. A modification in the middle of a file that preserves the original file size could therefore go undetected.

  • Warm-open memory is not zero. Although ByteJSON avoids materializing the entire JSON document, the children index, key dictionary, and lookup cache remain in memory.

Where this could go next

  • Native scanner: A Cython or Rust implementation could provide the largest performance improvement, since the Python-level scanner currently dominates cold index construction time.

  • Parallel index construction: Large files could potentially be indexed using multiple CPU cores.

  • Streaming index construction: Support for building indexes without the current whole-file mmap-based approach would make ByteJSON more suitable for substantially larger files.

  • Smaller indexes: Varint-encoded offsets, compressed offset representations, and more compact node records could significantly reduce .bjidx size.

  • JSONPath-style queries: Higher-level querying could provide more convenient access to large documents without requiring manual traversal of the lazy object/list hierarchy.

Design philosophy

ByteJSON does not try to be a faster replacement for Python's json module. Instead, it explores a different access model: treating a JSON document more like a random-access data store than a document that must be fully materialized before it can be queried.

Traditional JSON
────────────────────────────────────────
JSON file → parse everything → Python objects
                              ↓
                        high memory use


ByteJSON
────────────────────────────────────────
JSON file → index locations → lazy access
                              ↓
                       decode only what is used

The goal is simple: make large JSON documents randomly accessible without requiring the entire document to live as Python objects in memory.

Metadata

Release files for bytejson 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bytejson 0.1.0
File Size Uploaded Provenance Actions
bytejson-0.1.0.tar.gz 15.9 kB With provenance Details for bytejson-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for bytejson 0.1.0
File Interpreter ABI Platform Actions
bytejson-0.1.0-py3-none-any.whl Python 3 none any Details for bytejson-0.1.0-py3-none-any.whl

Total release size: 28.5 kB

Release files / bytejson-0.1.0.tar.gz

Download URL bytejson-0.1.0.tar.gz
Size 15.9 kB
Tags Source
SHA-256 checksum
How to use checksums
14807b6d9f272313a248787f13021d4d67546d61e0d309380760df178f2371fb
BLAKE2b-256 checksum
How to use checksums
559ffb525d9eecc7da46c34d98a3f13b50752ef3bca4c9267d9d8d6962ebe155
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 11, 2026.

Transparency log

Release files / bytejson-0.1.0-py3-none-any.whl

Download URL bytejson-0.1.0-py3-none-any.whl
Size 12.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e68fa50b9a8e59df92ea5feac462eec976d12998d02a62bf6640939ee90b1230
BLAKE2b-256 checksum
How to use checksums
1fe64eac01bf17fa1e0398741e2ee15e862642554f5f3cc44532e61447e77145
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 11, 2026.

Transparency log

Security for bytejson 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for bytejson-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 11, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page