Jsonline
Jsonline is a Python library for efficiently working with JSON Lines files. It allows you to access and append data without loading the entire file into memory, making it ideal for large datasets.
This library treats JSON Lines files as if they were read-only lists, but with an append method. It builds an index of the start and end positions of each JSON object in the file. When you access an element, Jsonline uses this index to read only the relevant line. The index is stored in a compressed gzip file with a .jsonl.idx extension for efficiency.
Features
- Memory-efficient: Reads only the data you need from the file.
- List-like access: Access JSON objects by index (
data[i]). - Fast appends: Efficiently append single or multiple objects.
- Automatic indexing: Creates and manages an index file for you.
- LRU Cache: Caches recently accessed objects in memory for faster retrieval.
- Context manager support: Works with the
withstatement for automatic resource management.
Installation
pip install jsonline
Usage
Opening a file
You can open a JSON Lines file using the JsonLine class or the jsonline.open() function. The .jsonl extension is not required in the file path.
from jsonline import JsonLine
# Using the JsonLine class
data = JsonLine('my_file')
# Or using the open function
import jsonline
data = jsonline.open('my_file')
If the file does not exist, it will be created with a .jsonl extension.
Appending data
You can append a single JSON object using the append method, or multiple objects using extend.
# Append a single object
data.append({'test': 1})
# Append multiple objects
data.extend([{'test': 2}, {'another_test': 3}])
Accessing data
You can access individual JSON objects by their index, just like a Python list.
# Get the first object
first_item = data[0]
# Get the last object
last_item = data[-1]
You can also iterate over the entire dataset:
for item in data:
print(item)
Context Manager
Jsonline supports the context manager protocol, which automatically closes the file for you.
with JsonLine('my_file') as data:
print(data[0])
API Reference
jsonline.JsonLine(path, cache_size=10, string_keys=True)
The main class for working with JSON Lines files.
path(str or pathlib.Path): Path to the JSON Lines file.cache_size(int, optional): The number of items to store in the LRU cache. Defaults to10.string_keys(bool, optional): IfFalse, allows non-string keys in JSON objects (this is non-standard). Defaults toTrue.
Methods
append(data): Appends a single JSON object to the end of the file.extend(data): Appends an iterable of JSON objects to the end of the file. This is more efficient than callingappendin a loop.get(index, default=None): Retrieves an item by its index. If the index is out of bounds, it returns thedefaultvalue.close(): Closes the file handle.rebuild_index(): Forces a rebuild of the index file. This can be useful if the file has been modified by another process.
jsonline.open(path, cache=10)
A convenience function for creating a JsonLine object.
path(str or pathlib.Path): Path to the JSON Lines file.cache(int, optional): The number of items to store in the LRU cache. Defaults to10.
jsonline.load(f, cache=10)
Loads a JsonLine object from an existing file-like object.
f(TextIO): A file-like object opened in text mode. This function uses the.nameattribute of the file-like object to open the file, so it will not work with in-memory objects likeio.StringIO.cache(int, optional): The number of items to store in the LRU cache. Defaults to10.
How it Works
Jsonline creates an index file (.jsonl.idx) that stores the byte offset and length of each line in the JSON Lines file. This allows for fast lookups without reading the entire file. When you request an item at a specific index, Jsonline reads the index file to find the position of that item, then seeks to that position in the data file and reads only the necessary bytes.
The index is automatically updated when you use append or extend. If the index file gets out of sync with the data file (e.g., if the data file is modified externally), you can use the rebuild_index() method to regenerate it.
Metadata
Release files for jsonline 0.3.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| jsonline-0.3.2.tar.gz | 5.4 kB | With provenance | Details for jsonline-0.3.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| jsonline-0.3.2-py3-none-any.whl | Python 3 | none | any | Details for jsonline-0.3.2-py3-none-any.whl |
Total release size: 11.5 kB
Release files / jsonline-0.3.2.tar.gz
| Download URL | jsonline-0.3.2.tar.gz |
|---|---|
| Size | 5.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6049060da2fe440d7c17d66556f1ebd11e20266d9c8a822c5b87c66760cb10fa
|
|
BLAKE2b-256 checksum How to use checksums |
c97ae1cf9923726229dfb516cd80600263ba868375cd2098cf163ad5dd2a3892
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
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 Jun 7, 2026.
Transparency logRelease files / jsonline-0.3.2-py3-none-any.whl
| Download URL | jsonline-0.3.2-py3-none-any.whl |
|---|---|
| Size | 6.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3dc079e799035fb748a6e78d53bbf9c8b31a0eda60ff9493c322e353178f52db
|
|
BLAKE2b-256 checksum How to use checksums |
7350a13e534f28d3929e7e647c4c74215b32339544ede53b97b5cc458ae09af2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
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 Jun 7, 2026.
Transparency logSecurity for jsonline 0.3.2
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
All 2 files in this release have been uploaded with provenance metadata.
Attestation for jsonline-0.3.2.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 Jun 7, 2026.
Transparency logA 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.