Skip to main content

A lightweight Python library for handling jsonlines files.

Project description

jsonl

CI pypi versions codecov license Linter: ruff Downloads

About

jsonl is a lightweight Python library designed to simplify working with JSON Lines data, adhering to the JSON Lines format.

Features

  • 🌎 Provides an API similar to Python's standard json module.
  • 🚀 Supports custom serialization/deserialization callbacks, with the standard json module as the default.
  • 🗜️ Supports compression and decompression using gzip, bzip2, and xz formats.
  • 🔧 Can load files with broken lines, skipping any malformed entries.
  • 📦 Includes an easy-to-use utility for writing to multiple JSON Lines files.
  • 📂 Supports loading multiple JSON Lines files from an archive (TAR or ZIP) using Unix shell-style wildcards to match filenames.

Installation

To install jsonl using pip, run the following command:

pip install py-jsonl

Getting Started

Dumping data to a JSON Lines File

Use jsonl.dump to incrementally write an iterable of dictionaries to a JSON Lines file:

import jsonl

data = [
    {"name": "Gilbert", "wins": [["straight", "7♣"], ["one pair", "10♥"]]},
    {"name": "May", "wins": []},
]

jsonl.dump(data, "file.jsonl")

Loading data from a JSON Lines File

Use jsonl.load to incrementally load a JSON Lines file into an iterable of objects:

import jsonl

iterable = jsonl.load("file.jsonl")
print(tuple(iterable))

Load multiple JSON Lines Files from an Archive (ZIP or TAR)

Use jsonl.load_archive to incrementally load multiple JSON Lines files from a ZIP or TAR archive. This function allows you to filter files using Unix shell-style wildcards.

import jsonl

# Load all JSON Lines files matching the pattern "*.jsonl" from the archive
for filename, items in jsonl.load_archive("path/to/archive.zip"):
    print("Filename:", filename)
    print("Data:", tuple(items))

Dumping data to Multiple JSON Lines Files

Use jsonl.dump_fork to incrementally write structured data to multiple .jsonl files—one per key (in this case, player name). This helps organize and efficiently store data for separate entities. This example creates individual JSON Lines files for each player, storing their respective wins.

import jsonl


def generate_win_data():
    """Yield player wins data for multiple players."""

    data = (
        {
            "name": "Gilbert",
            "wins": [
                {"hand": "straight", "card": "7♣"},
                {"hand": "one pair", "card": "10♥"},
            ]
        },
        {
            "name": "May",
            "wins": [
                {"hand": "two pair", "card": "9♠"},
            ]
        },
        {
            "name": "Gilbert",
            "wins": [
                {"hand": "three of a kind", "card": "A♦"},
            ]
        }
    )
    for player in data:
        name = player["name"]
        yield (f"{name}.jsonl", player["wins"])


# Write the generated data to files in JSON Lines format
jsonl.dump_fork(generate_win_data())

Documentation

For more detailed information and usage examples, refer to the project documentation

Development

To contribute to the project, you can run the following commands for testing and documentation:

First, ensure you have the latest version of pip:

python -m pip install --upgrade pip

Running Unit Tests

Install the development dependencies and run the tests:

pip install --group=test  # Install test dependencies
pytest tests/ # Run all tests
pytest --cov jsonl # Run tests with coverage

Running Linter

pip install --group=lint  # Install linter dependencies
ruff check . # Run linter

Building the Documentation

To build the documentation locally, use the following commands:

pip install --group=doc  # Install documentation dependencies
mkdocs serve # Start live-reloading docs server
mkdocs build # Build the documentation site

License

This project is licensed under the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py_jsonl-1.3.13.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

py_jsonl-1.3.13-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file py_jsonl-1.3.13.tar.gz.

File metadata

  • Download URL: py_jsonl-1.3.13.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.23

File hashes

Hashes for py_jsonl-1.3.13.tar.gz
Algorithm Hash digest
SHA256 1391f0aa5160e4fa5cabcfd41087d907f83153f30a3f6bb8f3c79a0422143925
MD5 b5ad6158fc9bc98f495e6976440c7481
BLAKE2b-256 f1b123e5ec5cb8d5ecbca5e578816545b3724fa00ce4266332c9aecae4fbe470

See more details on using hashes here.

File details

Details for the file py_jsonl-1.3.13-py3-none-any.whl.

File metadata

  • Download URL: py_jsonl-1.3.13-py3-none-any.whl
  • Upload date:
  • Size: 7.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.23

File hashes

Hashes for py_jsonl-1.3.13-py3-none-any.whl
Algorithm Hash digest
SHA256 db8a0c68a69f9775a12d42e57fadc8fee2f8f3ea6c133a4fe8b238a37496cc46
MD5 4b707f7ae79f96824cc059386419755e
BLAKE2b-256 054ed0bb707aec7e0edd59e63b23e40009f507b54b5f8a1d6613c07535c8b6fa

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page