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.

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))

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.12.tar.gz (8.2 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.12-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: py_jsonl-1.3.12.tar.gz
  • Upload date:
  • Size: 8.2 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.12.tar.gz
Algorithm Hash digest
SHA256 e8fe6733c97ac984c6a8f04bf6a85cfd3fb3ca8cd2ad49dcf3a0a5e6fcc4be35
MD5 2f94e202d00a9f29ffc27126287ea66c
BLAKE2b-256 d022c8f6959b9506efbb438b2f581e1025cb701a43beeb1e2d280e110732fb8c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: py_jsonl-1.3.12-py3-none-any.whl
  • Upload date:
  • Size: 6.1 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.12-py3-none-any.whl
Algorithm Hash digest
SHA256 72e993d8f20c5c9efb56209e04dc525a8271fbf87dec1c794bf3572678007522
MD5 747a56fb9ba960b20295d9b973e2a883
BLAKE2b-256 e992f4cef77cb90c88238886b05bbf858d4863fa41996ffe12a606a504f4e25d

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