Skip to main content

Iterative JSON parser with a standard Python iterator interface

Project description

https://travis-ci.org/ICRAR/ijson.svg?branch=master https://coveralls.io/repos/github/ICRAR/ijson/badge.svg?branch=master

ijson

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

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": { ... }},
      // ...
    ]
  }
}

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.

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>')

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-2.5.1.tar.gz (19.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-2.5.1-cp37-cp37m-manylinux1_x86_64.whl (61.2 kB view details)

Uploaded CPython 3.7m

ijson-2.5.1-cp37-cp37m-macosx_10_6_x86_64.whl (38.8 kB view details)

Uploaded CPython 3.7mmacOS 10.6+ x86-64

ijson-2.5.1-cp36-cp36m-manylinux1_x86_64.whl (61.2 kB view details)

Uploaded CPython 3.6m

ijson-2.5.1-cp36-cp36m-macosx_10_6_x86_64.whl (38.8 kB view details)

Uploaded CPython 3.6mmacOS 10.6+ x86-64

ijson-2.5.1-cp35-cp35m-manylinux1_x86_64.whl (61.2 kB view details)

Uploaded CPython 3.5m

ijson-2.5.1-cp35-cp35m-macosx_10_6_x86_64.whl (38.8 kB view details)

Uploaded CPython 3.5mmacOS 10.6+ x86-64

ijson-2.5.1-cp34-cp34m-manylinux1_x86_64.whl (61.0 kB view details)

Uploaded CPython 3.4m

ijson-2.5.1-cp34-cp34m-macosx_10_6_x86_64.whl (38.8 kB view details)

Uploaded CPython 3.4mmacOS 10.6+ x86-64

ijson-2.5.1-cp27-cp27mu-manylinux1_x86_64.whl (59.2 kB view details)

Uploaded CPython 2.7mu

ijson-2.5.1-cp27-cp27m-manylinux1_x86_64.whl (59.2 kB view details)

Uploaded CPython 2.7m

ijson-2.5.1-cp27-cp27m-macosx_10_6_x86_64.whl (38.7 kB view details)

Uploaded CPython 2.7mmacOS 10.6+ x86-64

File details

Details for the file ijson-2.5.1.tar.gz.

File metadata

  • Download URL: ijson-2.5.1.tar.gz
  • Upload date:
  • Size: 19.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.8

File hashes

Hashes for ijson-2.5.1.tar.gz
Algorithm Hash digest
SHA256 19ec46a2f7991004e5202ecee56c569616b8a7f95686ad7fd0a9ec81cac00269
MD5 eca8f5ec6fdc5071107d5411c99a8437
BLAKE2b-256 bbf99dd57fe468e033610b979adcb45b159b76309ea4df63f70a10cfb75d71ae

See more details on using hashes here.

File details

Details for the file ijson-2.5.1-cp37-cp37m-manylinux1_x86_64.whl.

File metadata

  • Download URL: ijson-2.5.1-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 61.2 kB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.8

File hashes

Hashes for ijson-2.5.1-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 d7baafc8027735d9525dc7e8275e2201d6ca91ead6b481caf31888615000394e
MD5 78a66cfdd3f9e0636b029b3ea5c7d8a8
BLAKE2b-256 92988350841674bc2c82cebbb122fa68b19c3e8f9702131cf0b53fe0c8cd4425

See more details on using hashes here.

File details

Details for the file ijson-2.5.1-cp37-cp37m-macosx_10_6_x86_64.whl.

File metadata

  • Download URL: ijson-2.5.1-cp37-cp37m-macosx_10_6_x86_64.whl
  • Upload date:
  • Size: 38.8 kB
  • Tags: CPython 3.7m, macOS 10.6+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.8

File hashes

Hashes for ijson-2.5.1-cp37-cp37m-macosx_10_6_x86_64.whl
Algorithm Hash digest
SHA256 2247d8906f948c2201d754d7d4aa4556e69de18c11d476c334e4c90fdd03b817
MD5 add4be6d9f5e33f8dc25783697308259
BLAKE2b-256 7185cc4b6d04950f40b06d86a4432b025db612a770ebfbb0d73fffcf8ac6fa5b

See more details on using hashes here.

File details

Details for the file ijson-2.5.1-cp36-cp36m-manylinux1_x86_64.whl.

File metadata

  • Download URL: ijson-2.5.1-cp36-cp36m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 61.2 kB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.8

File hashes

Hashes for ijson-2.5.1-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 8238b389fc87b64fcfac851ac409e53614a4390077d9a5ffbad1ba99a812d189
MD5 c28a78442775f296b2d6f22fa7ea446d
BLAKE2b-256 9a491f60e045f0d5b0087906e9b1dbd592714bc50dea2f7163e25047756f9a1c

See more details on using hashes here.

File details

Details for the file ijson-2.5.1-cp36-cp36m-macosx_10_6_x86_64.whl.

File metadata

  • Download URL: ijson-2.5.1-cp36-cp36m-macosx_10_6_x86_64.whl
  • Upload date:
  • Size: 38.8 kB
  • Tags: CPython 3.6m, macOS 10.6+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.8

File hashes

Hashes for ijson-2.5.1-cp36-cp36m-macosx_10_6_x86_64.whl
Algorithm Hash digest
SHA256 3cda11c1b4dcf5cdb4b2f8785ca7438a577ffc65ac6d9d235c419ed7d890a297
MD5 894e3ef8106b971821d917101db78ee9
BLAKE2b-256 afbea71d465a7382a7f2d54018a02ca697303a683faa2319d440f7ace17da049

See more details on using hashes here.

File details

Details for the file ijson-2.5.1-cp35-cp35m-manylinux1_x86_64.whl.

File metadata

  • Download URL: ijson-2.5.1-cp35-cp35m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 61.2 kB
  • Tags: CPython 3.5m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.8

File hashes

Hashes for ijson-2.5.1-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 e91057e18eedefeeaca05126493b6f2d1eeb43f45b1654b066b4cb296bc9bbde
MD5 a587610a247869d4a7c0698c19a2bc83
BLAKE2b-256 1306f62492778725fbbde4912bbfafb67d90f7beb511ca70685fcb3f0d4fba32

See more details on using hashes here.

File details

Details for the file ijson-2.5.1-cp35-cp35m-macosx_10_6_x86_64.whl.

File metadata

  • Download URL: ijson-2.5.1-cp35-cp35m-macosx_10_6_x86_64.whl
  • Upload date:
  • Size: 38.8 kB
  • Tags: CPython 3.5m, macOS 10.6+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.8

File hashes

Hashes for ijson-2.5.1-cp35-cp35m-macosx_10_6_x86_64.whl
Algorithm Hash digest
SHA256 bfbcd5b6f48b9a0b88845b90a5137f482b16fbaea79df8c4b6eeb9cc5dc0da20
MD5 e12a177d758da0f2e750459c26638cb6
BLAKE2b-256 c2469ed69384cdd24162c2fd1420120cc83854aa8ef41afc07e78b34d8f54d8b

See more details on using hashes here.

File details

Details for the file ijson-2.5.1-cp34-cp34m-manylinux1_x86_64.whl.

File metadata

  • Download URL: ijson-2.5.1-cp34-cp34m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 61.0 kB
  • Tags: CPython 3.4m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.8

File hashes

Hashes for ijson-2.5.1-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 d93e3b1ddb18507d79a7d8fb877ab8a5cd49e3284460c43020aad134af1c52e1
MD5 dbd846fa00ac8af2e7a9fa86ae22acee
BLAKE2b-256 b3f9c269ac55a319e57088919712b45c044994f270167d3eec440d4180db8c65

See more details on using hashes here.

File details

Details for the file ijson-2.5.1-cp34-cp34m-macosx_10_6_x86_64.whl.

File metadata

  • Download URL: ijson-2.5.1-cp34-cp34m-macosx_10_6_x86_64.whl
  • Upload date:
  • Size: 38.8 kB
  • Tags: CPython 3.4m, macOS 10.6+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.8

File hashes

Hashes for ijson-2.5.1-cp34-cp34m-macosx_10_6_x86_64.whl
Algorithm Hash digest
SHA256 147830257fbd3c379b5d9197ca6d62adf23cf3cd7bf447f9b8ba09ea4be2dbda
MD5 963ea29adf2933535a62dc0345b313cf
BLAKE2b-256 fd5aaa92b8526a74907607686d46f902bec2e1072debe6d14225725dc8b04d66

See more details on using hashes here.

File details

Details for the file ijson-2.5.1-cp27-cp27mu-manylinux1_x86_64.whl.

File metadata

  • Download URL: ijson-2.5.1-cp27-cp27mu-manylinux1_x86_64.whl
  • Upload date:
  • Size: 59.2 kB
  • Tags: CPython 2.7mu
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.8

File hashes

Hashes for ijson-2.5.1-cp27-cp27mu-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 53923e5a58e78a5eca8e461e2c6c8a7b01cfeb9e83dfce373de57991dccf20a9
MD5 b0da851f97f71dd9fdb2300a7081e5b2
BLAKE2b-256 544929a3bf4bdb8c4660c2706e305ac606ad7f1458f8bf7d61fbe8d3b1fd8cfa

See more details on using hashes here.

File details

Details for the file ijson-2.5.1-cp27-cp27m-manylinux1_x86_64.whl.

File metadata

  • Download URL: ijson-2.5.1-cp27-cp27m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 59.2 kB
  • Tags: CPython 2.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.8

File hashes

Hashes for ijson-2.5.1-cp27-cp27m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 240ccc575097784aaae447d29525801d27fb36020f8bfb7280668c37afe4d543
MD5 eb067f38d4cb3c834cb6e8320e63ab26
BLAKE2b-256 ed3b9caa5cd1e847a22af91a05b8db7ce4269e1065508944ccc56012f45e316d

See more details on using hashes here.

File details

Details for the file ijson-2.5.1-cp27-cp27m-macosx_10_6_x86_64.whl.

File metadata

  • Download URL: ijson-2.5.1-cp27-cp27m-macosx_10_6_x86_64.whl
  • Upload date:
  • Size: 38.7 kB
  • Tags: CPython 2.7m, macOS 10.6+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.8

File hashes

Hashes for ijson-2.5.1-cp27-cp27m-macosx_10_6_x86_64.whl
Algorithm Hash digest
SHA256 92ee4d4d3d0fd5ad59ec2ebc0ab0bd8a7b0eb79c8e21e87c18aa3dd282958714
MD5 9cf3c3f2b1c639e964964b9771671891
BLAKE2b-256 5e621456949e921c33e2d104dbfed5405f120162379dbfc226be7e71f525fe8c

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