Skip to main content

Iterative JSON parser with a standard Python iterator interface

Project description

https://travis-ci.org/isagalaev/ijson.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)

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

Backends

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

  • yajl2: wrapper around YAJL version 2.x

  • yajl: wrapper around YAJL version 1.x

  • python: pure Python parser (good to use under PyPy)

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

import ijson.backends.yajl2 as ijson

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

Importing the top level library as import ijson uses the pure Python backend.

Acknowledgements

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.0.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

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

ijson-2.0-py2.py3-none-any.whl (13.0 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: ijson-2.0.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for ijson-2.0.tar.gz
Algorithm Hash digest
SHA256 85bfc06b2bb492f205bc5dd9531ff0e64d2e18e03e64665934715d58331c1dd8
MD5 972a13b7db7756439fdd8f81bf5c90d3
BLAKE2b-256 aa6993c9e4d916b0bd1cb0a5186ae7550a025dcbe765938d39302c03a3484abd

See more details on using hashes here.

File details

Details for the file ijson-2.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for ijson-2.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 5ecb58c6e71f2ff79b3e80c3436f9fcd0faacb64e3acd298291e49c31be0d59d
MD5 036987152df2bf9aef644a91a812e076
BLAKE2b-256 cf10125224c6499b4862cdce6985993d163ea5c89628e7d0424b475a07535378

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