Skip to main content

Iterative JSON parser with a standard Python iterator interface

Project description

https://travis-ci.com/ICRAR/ijson.svg?branch=master https://coveralls.io/repos/github/ICRAR/ijson/badge.svg?branch=master https://badge.fury.io/py/ijson.svg https://img.shields.io/pypi/pyversions/ijson.svg https://img.shields.io/pypi/dd/ijson.svg https://img.shields.io/pypi/dw/ijson.svg https://img.shields.io/pypi/dm/ijson.svg

ijson

Ijson is an iterative JSON parser with a standard Python iterator interfaces.

Usage

All usage example will be using a JSON document describing geographical objects:

{
  "earth": {
    "europe": [
      {"name": "Paris", "type": "city", "info": { ... }},
      {"name": "Thames", "type": "river", "info": { ... }},
      // ...
    ],
    "america": [
      {"name": "Texas", "type": "state", "info": { ... }},
      // ...
    ]
  }
}

High-level interfaces

Most common usage is having ijson yield native Python objects out of a JSON stream located under a prefix. Here’s how to process all European cities:

import ijson

f = urlopen('http://.../')
objects = ijson.items(f, 'earth.europe.item')
cities = (o for o in objects if o['type'] == 'city')
for city in cities:
    do_something_with(city)

For how to build a prefix see the Prefix section below.

Other times it might be useful to iterate over object members rather than objects themselves (e.g., when objects are too big). In that case one can use the kvitems functions instead:

import ijson

f = urlopen('http://.../')
european_places = ijson.kvitems(f, 'earth.europe.item')
names = (v for k, v in european_places if k == 'name')
for name in names:
    do_something_with(name)

Lower-level interfaces

Sometimes when dealing with a particularly large JSON payload it may worth to not even construct individual Python objects and react on individual events immediately producing some result:

import ijson

parser = ijson.parse(urlopen('http://.../'))
stream.write('<geo>')
for prefix, event, value in parser:
    if (prefix, event) == ('earth', 'map_key'):
        stream.write('<%s>' % value)
        continent = value
    elif prefix.endswith('.name'):
        stream.write('<object name="%s"/>' % value)
    elif (prefix, event) == ('earth.%s' % continent, 'end_map'):
        stream.write('</%s>' % continent)
stream.write('</geo>')

Even more bare-bones is the ability to react on individual events without even calculating a prefix:

import ijson

events = ijson.basic_parse(urlopen('http://.../'))
num_names = sum(1 for event, value in events
                if event == 'map_key' and value == 'name')

asyncio support

In python 3.5+ all of the methods above have an *_async counterpart that works on file-like asynchronous objects, and that can be iterated asynchronously. In other words, something like this:

import asyncio
import ijson

async def run():
   f = await async_urlopen('http://..../')
   async for object in ijson.items_async(f, 'earth.europe.item'):
      if object['type'] == 'city':
         do_something_with(city)
asyncio.run(run())

Push interfaces

All examples above use a file-like object as the data input (both the normal case, and for asyncio support), and hence are “pull” interfaces, with the library reading data as necessary. If for whatever reason it’s not possible to use such method, you can still push data through yet a different interface: coroutines. Coroutines effectively allow users to send data to them at any point in time, with a final target coroutine-like object receiving the results.

In the following example the user is doing the reading instead of letting the library do it:

import ijson

@ijson.coroutine
def print_cities():
   while True:
      obj = (yield)
      if obj['type'] != 'city':
         continue
      print(obj)

coro = ijson.items_coro(print_cities(), 'earth.europe.item')
f = urlopen('http://.../')
chunk = f.read(buf_size)
while chunk:
   try:
      coro.send(chunk)
   except StopIteration:
         break
   chunk = f.read()

All four ijson iterators have a *_coro counterpart that work by pushing data into them. Instead of receiving a file-like object and option buffer size as arguments, they receive a single target argument, which should be a coroutine-like object (anything implementing a send method) through which results will be published.

Events

When using the lower-level ijson.parse function, three-element tuples are generated containing a prefix, an event name, and a value. Events will be one of the following:

  • start_map and end_map indicate the beginning and end of a JSON object, respectively. They carry a None as their value.

  • start_array and end_array indicate the beginning and end of a JSON array, respectively. They also carry a None as their value.

  • map_key indicates the name of a field in a JSON object. Its associated value is the name itself.

  • null, boolean, integer, double, number and string all indicate actual content, which is stored in the associated value.

Prefix

A prefix represents the context within a JSON document where an event originates at. It works as follows:

  • It starts as an empty string.

  • A <name> part is appended when the parser starts parsing the contents of a JSON object member called name, and removed once the content finishes.

  • A literal item part is appended when the parser is parsing elements of a JSON array, and removed when the array ends.

  • Parts are separated by ..

When using the ijson.items function, the prefix works as the selection for which objects should be automatically built and returned by ijson.

Backends

Ijson provides several implementations of the actual parsing in the form of backends located in ijson/backends:

  • yajl2_c: a C extension using YAJL 2.x. This is the fastest, but might require a compiler and the YAJL development files to be present when installing this package. Binary wheel distributions exist for major platforms/architectures to spare users from having to compile the package.

  • yajl2_cffi: wrapper around YAJL 2.x using CFFI.

  • yajl2: wrapper around YAJL 2.x using ctypes, for when you can’t use CFFI for some reason.

  • yajl: deprecated YAJL 1.x + ctypes wrapper, for even older systems.

  • python: pure Python parser, good to use with PyPy

You can import a specific backend and use it in the same way as the top level library:

import ijson.backends.yajl2_cffi as ijson

for item in ijson.items(...):
    # ...

Importing the top level library as import ijson uses the first available backend in the same order of the list above.

Acknowledgements

ijson was originally developed and actively maintained until 2016 by Ivan Sagalaev. In 2019 he handed over the maintenance of the project and the PyPI ownership.

Python parser in ijson is relatively simple thanks to Douglas Crockford who invented a strict, easy to parse syntax.

The YAJL library by Lloyd Hilaiel is the most popular and efficient way to parse JSON in an iterative fashion.

Ijson was inspired by yajl-py wrapper by Hatem Nassrat. Though ijson borrows almost nothing from the actual yajl-py code it was used as an example of integration with yajl using ctypes.

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

ijson-3.0rc2.tar.gz (38.3 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

ijson-3.0rc2-cp38-cp38-manylinux1_x86_64.whl (89.1 kB view details)

Uploaded CPython 3.8

ijson-3.0rc2-cp37-cp37m-manylinux1_x86_64.whl (83.2 kB view details)

Uploaded CPython 3.7m

ijson-3.0rc2-cp37-cp37m-macosx_10_6_x86_64.whl (31.3 kB view details)

Uploaded CPython 3.7mmacOS 10.6+ x86-64

ijson-3.0rc2-cp36-cp36m-manylinux1_x86_64.whl (83.2 kB view details)

Uploaded CPython 3.6m

ijson-3.0rc2-cp36-cp36m-macosx_10_6_x86_64.whl (31.3 kB view details)

Uploaded CPython 3.6mmacOS 10.6+ x86-64

ijson-3.0rc2-cp35-cp35m-manylinux1_x86_64.whl (83.2 kB view details)

Uploaded CPython 3.5m

ijson-3.0rc2-cp35-cp35m-macosx_10_6_x86_64.whl (31.3 kB view details)

Uploaded CPython 3.5mmacOS 10.6+ x86-64

ijson-3.0rc2-cp34-cp34m-manylinux1_x86_64.whl (82.8 kB view details)

Uploaded CPython 3.4m

ijson-3.0rc2-cp34-cp34m-macosx_10_6_x86_64.whl (31.2 kB view details)

Uploaded CPython 3.4mmacOS 10.6+ x86-64

ijson-3.0rc2-cp27-cp27mu-manylinux1_x86_64.whl (78.9 kB view details)

Uploaded CPython 2.7mu

ijson-3.0rc2-cp27-cp27m-manylinux1_x86_64.whl (78.9 kB view details)

Uploaded CPython 2.7m

ijson-3.0rc2-cp27-cp27m-macosx_10_6_x86_64.whl (31.1 kB view details)

Uploaded CPython 2.7mmacOS 10.6+ x86-64

File details

Details for the file ijson-3.0rc2.tar.gz.

File metadata

  • Download URL: ijson-3.0rc2.tar.gz
  • Upload date:
  • Size: 38.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.7.5

File hashes

Hashes for ijson-3.0rc2.tar.gz
Algorithm Hash digest
SHA256 ddd9a858828f4d9a1b895249acd47cef915a5e017ce618bef182026016fe6f1d
MD5 297fcab215693e4ea6a40369a4c33141
BLAKE2b-256 1d8d4e2e884b992df18aad7b7824e55ed72f41e66ca27aab0039b826e7ff21b7

See more details on using hashes here.

File details

Details for the file ijson-3.0rc2-cp38-cp38-manylinux1_x86_64.whl.

File metadata

  • Download URL: ijson-3.0rc2-cp38-cp38-manylinux1_x86_64.whl
  • Upload date:
  • Size: 89.1 kB
  • Tags: CPython 3.8
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.7.5

File hashes

Hashes for ijson-3.0rc2-cp38-cp38-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 d421f70f0fbabfee66f816cf46bbdfe984a99c428a20e57c178ef88678c0388b
MD5 aae489ccad367e99f0a7d6339c186228
BLAKE2b-256 4a3c0250c8eb30a9b179c120d41510cac06d56c7b65c02ce32567d475581f80a

See more details on using hashes here.

File details

Details for the file ijson-3.0rc2-cp37-cp37m-manylinux1_x86_64.whl.

File metadata

  • Download URL: ijson-3.0rc2-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 83.2 kB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.7.5

File hashes

Hashes for ijson-3.0rc2-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 fee834d6ffaa3c19d4c70792af79a3cc780fa3450da4369b1e14d91a7c7a87b5
MD5 c9ce4cc9e94d171d2b2abee47a399656
BLAKE2b-256 2db75a5a2ef0d04debb0cb036aef4028f7985f3cb277fa375bea6547b81f673f

See more details on using hashes here.

File details

Details for the file ijson-3.0rc2-cp37-cp37m-macosx_10_6_x86_64.whl.

File metadata

  • Download URL: ijson-3.0rc2-cp37-cp37m-macosx_10_6_x86_64.whl
  • Upload date:
  • Size: 31.3 kB
  • Tags: CPython 3.7m, macOS 10.6+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.7.5

File hashes

Hashes for ijson-3.0rc2-cp37-cp37m-macosx_10_6_x86_64.whl
Algorithm Hash digest
SHA256 eaeb24def11f6be44398b2e6446f4e58ae7b0c2e6a649da293fcd9f6a9a00b5a
MD5 d183e3177fe7999708e06f46dd00ed3d
BLAKE2b-256 743e32edb6f1aff0b765f09fb91f5b1653a893c04afb259f37338702ec19adec

See more details on using hashes here.

File details

Details for the file ijson-3.0rc2-cp36-cp36m-manylinux1_x86_64.whl.

File metadata

  • Download URL: ijson-3.0rc2-cp36-cp36m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 83.2 kB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.7.5

File hashes

Hashes for ijson-3.0rc2-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 60b1c620567858cfb2ca97db9d043d89ef5fb4b1fff43fabb3b03c2389784a9e
MD5 e2f8116c3f31ab413a9e7e55fa63f86f
BLAKE2b-256 c6371b646fe9ea3b66c331fd0a9d7285df7d44951e6c4dd2f6c5b287e755c372

See more details on using hashes here.

File details

Details for the file ijson-3.0rc2-cp36-cp36m-macosx_10_6_x86_64.whl.

File metadata

  • Download URL: ijson-3.0rc2-cp36-cp36m-macosx_10_6_x86_64.whl
  • Upload date:
  • Size: 31.3 kB
  • Tags: CPython 3.6m, macOS 10.6+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.7.5

File hashes

Hashes for ijson-3.0rc2-cp36-cp36m-macosx_10_6_x86_64.whl
Algorithm Hash digest
SHA256 f551c20a1e3c47667cafff5f1e7ea640b67769b295f8a937b3b7ee52d86d047b
MD5 d11fb709d081d839838b5ca21ad06dfc
BLAKE2b-256 5be4a11bdfb4dccae1fa642a68a9ae6ff52f8ac461cfae26338132ade19ed8c6

See more details on using hashes here.

File details

Details for the file ijson-3.0rc2-cp35-cp35m-manylinux1_x86_64.whl.

File metadata

  • Download URL: ijson-3.0rc2-cp35-cp35m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 83.2 kB
  • Tags: CPython 3.5m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.7.5

File hashes

Hashes for ijson-3.0rc2-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 40dc76736db15e4def4d0eb5e696606918008d03ed179132aa0960cd0dff2d3a
MD5 566651f7dbc749667a50a92394094d78
BLAKE2b-256 59b98bfec6eb5f5b7334d56818b622f804d79419ffff9b6187071b6252a00f0f

See more details on using hashes here.

File details

Details for the file ijson-3.0rc2-cp35-cp35m-macosx_10_6_x86_64.whl.

File metadata

  • Download URL: ijson-3.0rc2-cp35-cp35m-macosx_10_6_x86_64.whl
  • Upload date:
  • Size: 31.3 kB
  • Tags: CPython 3.5m, macOS 10.6+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.7.5

File hashes

Hashes for ijson-3.0rc2-cp35-cp35m-macosx_10_6_x86_64.whl
Algorithm Hash digest
SHA256 928d9594cd3d9f7858fa2586d0a9bc58fde71f2a968d2a5b3ea46fd35aa80fd1
MD5 2d50264b5e97feb753f9e2be7b5836cb
BLAKE2b-256 587ea1dba83e33a45d6921f946f306b8580dcdf65e974fb45763ab9dbd00d11a

See more details on using hashes here.

File details

Details for the file ijson-3.0rc2-cp34-cp34m-manylinux1_x86_64.whl.

File metadata

  • Download URL: ijson-3.0rc2-cp34-cp34m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 82.8 kB
  • Tags: CPython 3.4m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.7.5

File hashes

Hashes for ijson-3.0rc2-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 cae0ece368ff266b8b9af2a26bd42cc04b87db074143860ffc0fca3b00fe2500
MD5 e5c9a8d656a666eb1c0747ef3d49d54b
BLAKE2b-256 6dff7ac4b4f75127445acce6150c5029bc047290969c78f3cf1e8acb8fab4662

See more details on using hashes here.

File details

Details for the file ijson-3.0rc2-cp34-cp34m-macosx_10_6_x86_64.whl.

File metadata

  • Download URL: ijson-3.0rc2-cp34-cp34m-macosx_10_6_x86_64.whl
  • Upload date:
  • Size: 31.2 kB
  • Tags: CPython 3.4m, macOS 10.6+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.7.5

File hashes

Hashes for ijson-3.0rc2-cp34-cp34m-macosx_10_6_x86_64.whl
Algorithm Hash digest
SHA256 70bc6d8cfea57d3297493804b47328105e2b43ba1235148f5e0f4630b5c7422e
MD5 be4dee8721df8259a4898e03b382b9a4
BLAKE2b-256 8ea68fd7bfb0af84efbbcb2a1488cb9d6e38064c932354071b8b18722f2130ff

See more details on using hashes here.

File details

Details for the file ijson-3.0rc2-cp27-cp27mu-manylinux1_x86_64.whl.

File metadata

  • Download URL: ijson-3.0rc2-cp27-cp27mu-manylinux1_x86_64.whl
  • Upload date:
  • Size: 78.9 kB
  • Tags: CPython 2.7mu
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.7.5

File hashes

Hashes for ijson-3.0rc2-cp27-cp27mu-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 b68085c9d19f7aac7cd28335a51fbb4582b0b7ed55caccfe097ca17fcd26d63d
MD5 7b5bfb37f22ba18abd0afed30f44b087
BLAKE2b-256 0f24632cc21348bda9685b3f5e2da66c36cee52c584535ad7f2ebe4abd3fb65d

See more details on using hashes here.

File details

Details for the file ijson-3.0rc2-cp27-cp27m-manylinux1_x86_64.whl.

File metadata

  • Download URL: ijson-3.0rc2-cp27-cp27m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 78.9 kB
  • Tags: CPython 2.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.7.5

File hashes

Hashes for ijson-3.0rc2-cp27-cp27m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 f791a6e72893791b9fd4790abf5695029422be449f0cea5aae310f81791b11df
MD5 93a5c32c4d1c09a70e133aa8e1c42f91
BLAKE2b-256 ffe75ad40033d3a9c35f657727df57fdfb353d4d6967efdecf23c08a0844a268

See more details on using hashes here.

File details

Details for the file ijson-3.0rc2-cp27-cp27m-macosx_10_6_x86_64.whl.

File metadata

  • Download URL: ijson-3.0rc2-cp27-cp27m-macosx_10_6_x86_64.whl
  • Upload date:
  • Size: 31.1 kB
  • Tags: CPython 2.7m, macOS 10.6+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.7.5

File hashes

Hashes for ijson-3.0rc2-cp27-cp27m-macosx_10_6_x86_64.whl
Algorithm Hash digest
SHA256 7c9266b304b59b28faddd1eb6e53fed9e2f4a311605f8883a3538449d1e15603
MD5 1962aa0c76741893b91ee6c92ddb5f25
BLAKE2b-256 dba2c95e339fb26ac4764f7ea3ec63a5f08aad9b2650e519dfde222c928adcb5

See more details on using hashes here.

Supported by

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