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

jsonlines

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

Features

Installation

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:`jsonlines.open()` takes a file name and returns either a reader or writer, making simple cases extremely simple:

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

with jsonlines.open('output.jsonl', mode='w') as writer:
    writer.write(...)

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

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

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:`Reader.read` 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)
writer.write(...)
writer.write_all([
    ...,
    ...,
    ...,
])
writer.close()
fp.close()

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:
    writer.write(...)
fp.close()

Note that the :py:func:`jsonlines.open()` 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 jsonlines.open('input.jsonl') as reader:
    ...

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

API

.. autofunction:: jsonlines.open

.. autoclass:: jsonlines.Reader
   :members:
   :inherited-members:

.. autoclass:: jsonlines.Writer
   :members:
   :inherited-members:

.. autoclass:: jsonlines.Error
   :members:

.. autoclass:: jsonlines.InvalidLineError
   :members:


Contributing

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

https://github.com/wbolster/jsonlines

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

License