Skip to main content

A Python wrapper to YAJL providing standard iterator interface to streaming JSON parsing

Project description

Ijson is a Python wrapper to YAJL which is a streaming SAX-like JSON parser. Ijson provides a standard Python iterator interface for it.

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:

from ijson import items

f = urlopen('http://.../')
objects = 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:

from ijson import parse

f = urlopen('http://.../')
parser = parse(f)
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>')

Acknowledgements

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

Uploaded Source

File details

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

File metadata

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

File hashes

Hashes for ijson-0.7.0.tar.gz
Algorithm Hash digest
SHA256 f7ba7abacbbc48e2235327910b3d0377277cc5747d03c43e94e9aa330f184cb5
MD5 c11cf7d81e1be61c193b403cd4a3ed48
BLAKE2b-256 1cd488c57a38ebf0e053ee0c74c21b78e872ee84f171d906ffddd15ddebbec62

See more details on using hashes here.

Supported by

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