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:

Running Unit Tests

Install the development dependencies and run the tests:

pip install ".[dev]"  # Skip if already installed
pytest tests/ # Run all tests
pytest --cov jsonl # Run tests with coverage

Running Linter

pip install ".[linter]"  # Skip if already installed
ruff check . # Run linter

Building the Documentation

To build the documentation locally, use the following commands:

pip install ".[doc]"  # Skip if already installed
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.11.tar.gz (8.4 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.11-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for py_jsonl-1.3.11.tar.gz
Algorithm Hash digest
SHA256 db25a517445e757b37b5bb6cda731618a201833e0ce543c565ccd7783605cce0
MD5 98a78fa3b551d903d4a1586954b7c0f1
BLAKE2b-256 048c92a5dd3c8decb09225a4ce69ac9aa0e938f84f434c7b5a7ec80dac6ee148

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for py_jsonl-1.3.11-py3-none-any.whl
Algorithm Hash digest
SHA256 d7301430108675f0385690f0773bd263e20151dce354529f378693cbf3b4876d
MD5 a6b4736c216965eece4b7dff45959981
BLAKE2b-256 669e8e1e901b68a470c45b0abdae49fc640f8798405d97e9fb8f803833b11602

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