File: index.rst

package info (click to toggle)
python-jsonlines 4.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 136 kB
  • sloc: python: 706; makefile: 2
file content (285 lines) | stat: -rw-r--r-- 7,868 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
=========
jsonlines
=========

.. py:currentmodule:: jsonlines

``jsonlines`` is a Python library to simplify working with jsonlines_
and ndjson_ data.

.. _jsonlines: http://jsonlines.org/
.. _ndjson: http://ndjson.org/

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

* Sensible behaviour for most use cases

  * transparently handles ``str`` and ``bytes``, both for input and output
  * supports multiple JSON libraries, e.g. ``json`` (standard library), ``orjson``, ``ujson``
  * transparently handles UTF-8 BOM (if present)
  * useful error messages
  * prevents gotchas, e.g. uses standard-compliant line breaking, unlike `str.splitlines`_

  .. _str.splitlines: https://docs.python.org/3/library/stdtypes.html#str.splitlines

* Convenient :py:func:`~jsonlines.open()` function

  * makes simple cases trivial to write
  * takes a file name and a mode
  * returns either a :py:class:`~jsonlines.Reader` or
    :py:class:`~jsonlines.Writer` instance
  * can be used as a context manager

* Flexible :py:class:`~jsonlines.Reader`

  * wraps a file-like object or any other iterable yielding lines
  * can read lines directly via the
    :py:meth:`~jsonlines.Reader.read()` method
  * can be used as an iterator, either directly or via the
    :py:meth:`~jsonlines.Reader.iter()` method
  * can validate data types, including `None` checks
  * can skip invalid lines during iteration
  * provides decent error messages
  * can be used as a context manager
  * allows complete control over decoding using a custom ``loads``
    callable

* Flexible :py:class:`~jsonlines.Writer`

  * wraps a file-like object
  * can produce compact output
  * can sort keys (deterministic output)
  * can flush the underlying stream after each write
  * can be used as a context manager
  * allows complete control over encoding using a custom ``dumps``
    callable


Installation
============

::

  pip install jsonlines

The supported Python versions are 3.8+.


User guide
==========

Import the ``jsonlines`` module to get started:

.. code-block:: python

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

* 4.0.0, released at 2023-09-01

  * use ‘orjson’ or ‘ujson’ for reading if available
    (`#81 <https://github.com/wbolster/jsonlines/pull/81>`_)

  * drop support for end-of-life Python versions; this package is now
    Python 3.8+ only.
    (`#80 <https://github.com/wbolster/jsonlines/pull/80>`_,
    `#80 <https://github.com/wbolster/jsonlines/pull/85>`_)

* 3.1.0, released at 2022-07-01

  * Return number of chars/bytes written by :py:meth:`Writer.write()`
    and :py:meth:`~Writer.write_all()`
    (`#73 <https://github.com/wbolster/jsonlines/pull/73>`_)

  * allow ``mode='x'`` in :py:func:`~jsonlines.open()`
    to open a file for exclusive creation
    (`#74 <https://github.com/wbolster/jsonlines/issues/74>`_)

* 3.0.0, released at 2021-12-04

  * add type annotations; adopt mypy in strict mode
    (`#58 <https://github.com/wbolster/jsonlines/pull/58>`_,
    `#62 <https://github.com/wbolster/jsonlines/pull/62>`_)

  * ignore UTF-8 BOM sequences in various scenarios
    (`#69 <https://github.com/wbolster/jsonlines/pull/69>`_)

  * support ``dumps()`` callables returning bytes again
    (`#64 <https://github.com/wbolster/jsonlines/issues/64>`_)

  * add basic support for rfc7464 text sequences
    (`#61 <https://github.com/wbolster/jsonlines/pull/61>`_)

  * drop support for ``numbers.Number`` in ``type=`` arguments
    (`#63 <https://github.com/wbolster/jsonlines/issues/63>`_)

* 2.0.0, released at 2021-01-04

  * drop support for end-of-life Python versions; this package is now
    Python 3.6+ only.
    (`#54 <https://github.com/wbolster/jsonlines/pull/54>`_,
    `#51 <https://github.com/wbolster/jsonlines/pull/51>`_)

* 1.2.0, released at 2017-08-17

  * allow ``mode='a'`` in :py:func:`~jsonlines.open()`
    to allow appending to an existing file
    (`#31 <https://github.com/wbolster/jsonlines/pull/31>`_)

* 1.1.3, released at 2017-07-19

  * fix incomplete iteration when given list containing empty strings
    (`#30 <https://github.com/wbolster/jsonlines/issues/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 <https://github.com/wbolster/jsonlines/issues/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 Python 2.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
=======

.. include:: ../LICENSE.rst