Switch branches/tags
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
231 lines (158 sloc) 5.62 KB


.. py:currentmodule:: jsonlines

jsonlines is a Python library to simplify working with jsonlines and ndjson data.

This data format is straight-forward: it is simply one valid JSON value per line, encoded using UTF-8. While code to consume and create such data is not that complex, it quickly becomes non-trivial enough to warrant a dedicated library when adding data validation, error handling, support for both binary and text streams, and so on. This small library implements all that (and more!) so that applications using this format do not have to reinvent the wheel.



pip install jsonlines

The supported Python versions are 3.3+ and Python 2.7.

User guide

Import the jsonlines module to get started:

import jsonlines

The convenience function :py:func:`` takes a file name and returns either a reader or writer, making simple cases extremely simple:

with'input.jsonl') as reader:
    for obj in reader:

with'output.jsonl', mode='w') as writer:

A :py:class:`Reader` typically wraps a file-like object:

fp = io.BytesIO(...)  # readable file-like object
reader = jsonlines.Reader(fp)
first =
second =

Instead of a file-like object, any iterable yielding JSON encoded strings can be provided:

lines = ['1', '2', '3']
reader = jsonlines.Reader(lines)

While the :py:meth:`` method can be used directly, it is often more convenient to use iteration:

for obj in reader:

Custom iteration flags, such as type checks, can be specified by calling :py:meth:`Reader.iter()` instead:

for obj in reader.iter(type=dict, skip_invalid=True):

A :py:class:`Writer` wraps a file-like object, and can write a single object, or multiple objects at once:

fp = io.BytesIO()  # writable file-like object
writer = jsonlines.Writer(fp)

Both readers and writers can be used as a context manager, in which case they will be closed automatically. Note that this will not close a passed-in file-like object since that object’s life span is controlled by the calling code. Example:

fp = io.BytesIO()  # file-like object
with jsonlines.Writer(fp) as writer:

Note that the :py:func:`` function does close the opened file, since the open file is not explicitly opened by the calling code. That means no .close() is needed there:

with'input.jsonl') as reader:

This should be enough to get started. See the API docs below for more details.


.. autofunction::

.. autoclass:: jsonlines.Reader

.. autoclass:: jsonlines.Writer

.. autoclass:: jsonlines.Error

.. autoclass:: jsonlines.InvalidLineError


The source code and issue tracker for this package can be found on Github:

Version history

  • 1.2.0, released at 2017-08-17
  • 1.1.3, released at 2017-07-19
    • fix incomplete iteration when given list containing empty strings (#30)
  • 1.1.2, released at 2017-06-26
    • documentation tweaks
    • enable building universal wheels
  • 1.1.1, released at 2017-06-04
    • include licensing information in sdist (#27)
    • doc tweaks
  • 1.1.0, released at 2016-10-07
    • rename first argument to :py:class:`Reader` since it is not required to be a file-like object
    • actually check that the reader/writer is not closed when performing operations
    • improved repr() output
    • doc tweaks
  • 1.0.0, released at 2016-10-05
    • minimum python versions are python 3.4+ and python2.7+
    • implemented lots of configuration options
    • add proper exceptions handling
    • add proper documentation
    • switch to semver
  • 0.0.1, released at 2015-03-02
    • initial release with basic functionality