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

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+ one can also directly use asynchronously-read objects in the context of an asyncio event loop, like so:

import ijson

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

In python 3.5+ all of the methods above have an *_async counterpart that works on asynchronous I/O 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.0rc1.tar.gz (37.1 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.0rc1-cp38-cp38-manylinux1_x86_64.whl (88.8 kB view details)

Uploaded CPython 3.8

ijson-3.0rc1-cp37-cp37m-manylinux1_x86_64.whl (84.8 kB view details)

Uploaded CPython 3.7m

ijson-3.0rc1-cp37-cp37m-macosx_10_6_x86_64.whl (31.4 kB view details)

Uploaded CPython 3.7mmacOS 10.6+ x86-64

ijson-3.0rc1-cp36-cp36m-manylinux1_x86_64.whl (84.8 kB view details)

Uploaded CPython 3.6m

ijson-3.0rc1-cp36-cp36m-macosx_10_6_x86_64.whl (31.4 kB view details)

Uploaded CPython 3.6mmacOS 10.6+ x86-64

ijson-3.0rc1-cp35-cp35m-manylinux1_x86_64.whl (84.8 kB view details)

Uploaded CPython 3.5m

ijson-3.0rc1-cp35-cp35m-macosx_10_6_x86_64.whl (31.4 kB view details)

Uploaded CPython 3.5mmacOS 10.6+ x86-64

ijson-3.0rc1-cp34-cp34m-manylinux1_x86_64.whl (84.4 kB view details)

Uploaded CPython 3.4m

ijson-3.0rc1-cp34-cp34m-macosx_10_6_x86_64.whl (31.4 kB view details)

Uploaded CPython 3.4mmacOS 10.6+ x86-64

ijson-3.0rc1-cp27-cp27mu-manylinux1_x86_64.whl (80.1 kB view details)

Uploaded CPython 2.7mu

ijson-3.0rc1-cp27-cp27m-manylinux1_x86_64.whl (80.1 kB view details)

Uploaded CPython 2.7m

ijson-3.0rc1-cp27-cp27m-macosx_10_6_x86_64.whl (31.2 kB view details)

Uploaded CPython 2.7mmacOS 10.6+ x86-64

File details

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

File metadata

  • Download URL: ijson-3.0rc1.tar.gz
  • Upload date:
  • Size: 37.1 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.0rc1.tar.gz
Algorithm Hash digest
SHA256 69de3668ef5aa19148422c197272eb99261a62fda0732d1de0bdcc5f4d4d4679
MD5 548e21dfc7fd17996ff6dfec84876977
BLAKE2b-256 b18b38d5bf0f1872ba1bb278f4d88991fca7cb69be3e03067b5d3cefa2ae2d1d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ijson-3.0rc1-cp38-cp38-manylinux1_x86_64.whl
  • Upload date:
  • Size: 88.8 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.0rc1-cp38-cp38-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 a5a3f6cd134c830c91e9e0de188cab0359eea81b8c93e21194b868e8ab95b3df
MD5 b28bf078652c7b6b1b1d5ccff9b560c7
BLAKE2b-256 470ef87f6a30f8e9cd5a400f2b8a9b168c4514f60e50bcaf95ffc56e15b7bd62

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ijson-3.0rc1-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 84.8 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.0rc1-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 b8732c4ff2016c634fc1bf83e7198f33637bc929a89d3385efd3d1dd29b2066e
MD5 49f8e3afbb2dfbd7e30324469c75013c
BLAKE2b-256 388936c9480d8711ae810ed6d3a3b99c1fdfc208a9ea84e577f355ced538a5df

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ijson-3.0rc1-cp37-cp37m-macosx_10_6_x86_64.whl
  • Upload date:
  • Size: 31.4 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.0rc1-cp37-cp37m-macosx_10_6_x86_64.whl
Algorithm Hash digest
SHA256 9643f556f6879cbc612b08e85f3ba135debb6e8e1edd1f9fc331b113d25db866
MD5 578b4a34d6d33cbda63a8dea8d800290
BLAKE2b-256 85fa78c3bc4a2c9a3d36f648daa1c18899501a5173874dac255365e232d5fdc1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ijson-3.0rc1-cp36-cp36m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 84.8 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.0rc1-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 3b5a5be88da5f8c1e98aabe6d1bbd1bcc568f29630906f7d4fa79e29aa0ae338
MD5 aec60d74ddba695767953754c701d59f
BLAKE2b-256 b7cfa291bd4c64e5416c308959bb3e305e9a30266153c5b0dfdd7408cac98ba4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ijson-3.0rc1-cp36-cp36m-macosx_10_6_x86_64.whl
  • Upload date:
  • Size: 31.4 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.0rc1-cp36-cp36m-macosx_10_6_x86_64.whl
Algorithm Hash digest
SHA256 89dffe53beafce857de2109f756be7cc4c68972320e48f20d8169d02a26a0fbf
MD5 8ae5ceb82c60e4d58bdafc6db1726fc8
BLAKE2b-256 d8b6d881a7c4beb36c980d4923e5cac41895102fc34e45c86eb904b87e86a059

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ijson-3.0rc1-cp35-cp35m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 84.8 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.0rc1-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 059cdf169e57a6eeb0c30503d0dbd3efa99dc11be33e861f4ac7495331713bc7
MD5 5de3320a814408e1f0bdea536e7e86e6
BLAKE2b-256 1b26d359b5d9f698303a6e21b3404df71e51e421d05f962bfff0187a762aeeb6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ijson-3.0rc1-cp35-cp35m-macosx_10_6_x86_64.whl
  • Upload date:
  • Size: 31.4 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.0rc1-cp35-cp35m-macosx_10_6_x86_64.whl
Algorithm Hash digest
SHA256 a91db000d11d902f794ab8419e7eaf3bd550f767322239f920403fa08d615a0b
MD5 e9f6ee5e98fe3a8b126b6ed0e41dd771
BLAKE2b-256 512ce8b3a85c9f20f4a2ca785e9fe85085d3aa9a8b3ea5d522a719cd7bab7900

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ijson-3.0rc1-cp34-cp34m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 84.4 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.0rc1-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 9edec541c74c8623d104e33131f2c8f9c54b2633982e3c0800e226e6f7c2dc7d
MD5 ea684ca2fc84dbd0b5e3e4298f6fe98d
BLAKE2b-256 0eb6dd1058b1906def9fa9664a0550c82f61e5bdb6a70820c1861c39849bb7f9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ijson-3.0rc1-cp34-cp34m-macosx_10_6_x86_64.whl
  • Upload date:
  • Size: 31.4 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.0rc1-cp34-cp34m-macosx_10_6_x86_64.whl
Algorithm Hash digest
SHA256 a1196bbbf250ea35063be18186a593b21aa4273577c760143c2c34d63bbd0584
MD5 edb2c930fec6c4cfda8d7eb6abad9bd9
BLAKE2b-256 53cd8c27d53d491b54ea59a557f504c7171c4f1a070a8ec7b28c25b4ed2bbd27

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ijson-3.0rc1-cp27-cp27mu-manylinux1_x86_64.whl
  • Upload date:
  • Size: 80.1 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.0rc1-cp27-cp27mu-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 f9930fa276376d9f6c1df3d31539cc57875806344212331cb65cd8acdf150db7
MD5 0d2e6cc060f1a2455424cc0ae3e1a696
BLAKE2b-256 a3381f8fee1bc9e9e15d84859f0288c91a08b66fe75df13d564edf8e06de524c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ijson-3.0rc1-cp27-cp27m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 80.1 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.0rc1-cp27-cp27m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 5d1edef1a9f12fdf1fe769175f5ffbab2e441780800da2051eefe60792fc8162
MD5 8bf31e37e2d24599d2836560a9406fe0
BLAKE2b-256 f8015951a0feee520c1dd53663836248ebe5ca9df824528147138eef92adf616

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ijson-3.0rc1-cp27-cp27m-macosx_10_6_x86_64.whl
  • Upload date:
  • Size: 31.2 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.0rc1-cp27-cp27m-macosx_10_6_x86_64.whl
Algorithm Hash digest
SHA256 db0b53568db72e8e8500dec84fe2da2db6858ce4865057ca1e119cba9398378d
MD5 08d7c59d55cef4030cb7e193e88e4041
BLAKE2b-256 122dce44f315c716167747a497fa9d6ea7a377bcc614356d81474ddfc25c7e3b

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