Skip to main content

A simple, fast and lightweight Python library for loading, saving, streaming and appending jsonl (also known as JSON Lines) files.

Project description

orjsonl

orjsonl is a simple, fast and lightweight Python library for loading, saving, streaming and appending jsonl (also known as JSON Lines) files. It is powered by orjson, the quickest and most correct json serializer currently available for Python.

Installation

orjsonl may be installed with pip:

pip install orjsonl

Usage

This code snippet demonstrates how jsonl files can be loaded, saved, appended and streamed with the load(), save(), append() and stream() functions, respectively:

>>> import orjsonl
>>> data = [
    {'hello' : 'world'},
    [1.1, 2.2, 3.3],
    42,
    True,
    None
]
>>> orjsonl.save(path='test.jsonl', data=data)
>>> orjsonl.load(path='test.jsonl')
[{'hello': 'world'}, [1.1, 2.2, 3.3], 42, True, None]
>>> orjsonl.append(path='test.jsonl', data=[('a', 'b', 'c')])
>>> [object_ for object_ in orjsonl.stream(path='test.jsonl')]
[{'hello': 'world'}, [1.1, 2.2, 3.3], 42, True, None, ['a', 'b', 'c']]

Load

def load(
    path: str | bytes | int | os.PathLike
) -> list[dict | list | int | float | str | bool | None]: ...

load() deserializes a UTF-8-encoded jsonl file to a list of Python objects.

path is a path-like object giving the pathname (absolute or relative to the current working directory), or an integer file descriptor, of the jsonl file to be deserialized.

This function returns a list object comprised of deserialized dict, list, int, float, str, bool or None objects.

Stream

def stream(
    path: str | bytes | int | os.PathLike
) -> map: ...

stream() creates a map object that deserializes a UTF-8-encoded jsonl file to Python objects.

path is a path-like object giving the pathname (absolute or relative to the current working directory), or an integer file descriptor, of the jsonl file to be deserialized by the map object.

This function returns a map object that deserializes the jsonl file to dict, list, int, float, str, bool or None objects.

Save

def save(
    path: str | bytes | int | os.PathLike,
    data: Iterable,
    default: Callable = None,
    option: int = 0
) -> None: ...

save() serializes an iterable of Python objects to a UTF-8-encoded jsonl file.

path is a path-like object giving the pathname (absolute or relative to the current working directory), or an integer file descriptor, of the jsonl file to be saved.

data is an iterable of Python objects to be serialized to the jsonl file.

default is an optional callable passed to orjson.dumps() as the default argument that serializes a subclass or arbitrary types to a supported type.

option is an optional integer passed to orjson.dumps() as the option argument that modifies how data is serialized.

Append

def append(
    path: str | bytes | int | os.PathLike,
    data: Iterable,
    newline: bool = True,
    default: Callable = None,
    option: int = 0
) -> None: ...

append() serializes and appends an iterable of Python objects to a UTF-8-encoded jsonl file.

path is a path-like object giving the pathname (absolute or relative to the current working directory), or an integer file descriptor, of the jsonl file to be appended.

data is an iterable of Python objects to be serialized and appended to the jsonl file.

newline is an optional Boolean flag that, if set to False, indicates that the jsonl file does not end with a newline and should, therefore, have one added before data is appended.

default is an optional callable passed to orjson.dumps() as the default argument that serializes a subclass or arbitrary types to a supported type.

option is an optional integer passed to orjson.dumps() as the option argument that modifies how data is serialized.

License

This library 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

orjsonl-0.1.3.tar.gz (6.1 kB view hashes)

Uploaded Source

Built Distribution

orjsonl-0.1.3-py3-none-any.whl (4.8 kB view hashes)

Uploaded Python 3

Supported by

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