Skip to main content
PyPI Version Build Status Code Coverage

This is a tiny library for reading JSON lines (.jl) files, including gzipped and broken files.

JSON lines is a text file format where each line is a single json encoded item.

Why?

Reading a well-formed JSON lines file is a one-liner in Python. But the file can be broken: cut at some point (this happens when the process writing it is killed), or concatenated from several cut pieces (this happend when the process started appending to the same file again). Handling all this cases is not easy, especially if the file is compressed.

json-lines handles all this cases for you!

Installation

pip install json-lines

If ujson is installed, it is used to speed up json decoding (which is the main performance bottleneck even for gzipped files).

Usage

In order to read a well-formed json lined file, pass an open file as the first argument to json_lines.reader. The file can be opened in text or binary mode, but if it’s opened in text mode, the encoding must be set correctly:

import json_lines

with open('file.jl', 'rb') as f:
    for item in json_lines.reader(f):
        print(item['x'])

There is also a helper function json_lines.open that recognizes “.gz” and “.gzip” extensions and opens them with gzip:

with json_lines.open('file.jl.gz') as f:
    for item in f:
        print(item['x'])

Handling broken (cut at some point) files is enabled by passing broken=True to json_lines.reader or json_lines.open. Broken lines are skipped (only logging a warning), and reading continues from the next valid position. This works both for compressed and uncompressed files:

with json_lines.open('file.jl.gz', broken=True) as f:
    for item in f:
        print(item['x'])

License

License is MIT.

Release files for json-lines 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for json-lines 0.5.0
File Size Uploaded
json-lines-0.5.0.tar.gz 4.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for json-lines 0.5.0
File Interpreter ABI Platform
json_lines-0.5.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 11.1 kB

Release files / json-lines-0.5.0.tar.gz

Download URL json-lines-0.5.0.tar.gz
Size 4.3 kB
Tags Source
SHA-256 checksum
How to use checksums
a1a5c3e24fb1bafc54b2bf273469c7b986445df520a923db182a96a954b42ae6
BLAKE2b-256 checksum
How to use checksums
adfc53be3bd1997800228c3dfc3b5b72b758263f8e08aa12429c1f72030688b9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.18.4 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.6.1

Release files / json_lines-0.5.0-py2.py3-none-any.whl

Download URL json_lines-0.5.0-py2.py3-none-any.whl
Size 6.8 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
8a20de205b582c9feb6fc2e0c3eafd849fd28c47432f6b3712b365f5068414bb
BLAKE2b-256 checksum
How to use checksums
7f0f79c96c0d26b276c583484fe8209e5ebbb416a920309568650325f6e1de73
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.18.4 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.6.1

Release history Release notifications | RSS feed

This release

0.5.0 This release

2 release files

0.4.0

2 release files

0.3.1

1 release file

0.3.0

1 release file

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page