4 Summary: Iterative JSON parser with a standard Python iterator interface
5 Home-page: https://github.com/isagalaev/ijson
7 Author-email: maniac@softwaremaniacs.org
13 Ijson is an iterative JSON parser with a standard Python iterator interface.
19 All usage example will be using a JSON document describing geographical
25 {"name": "Paris", "type": "city", "info": { ... }},
26 {"name": "Thames", "type": "river", "info": { ... }},
30 {"name": "Texas", "type": "state", "info": { ... }},
36 Most common usage is having ijson yield native Python objects out of a JSON
37 stream located under a prefix. Here's how to process all European cities::
41 f = urlopen('http://.../')
42 objects = ijson.items(f, 'earth.europe.item')
43 cities = (o for o in objects if o['type'] == 'city')
45 do_something_with(city)
47 Sometimes when dealing with a particularly large JSON payload it may worth to
48 not even construct individual Python objects and react on individual events
49 immediately producing some result::
53 parser = ijson.parse(urlopen('http://.../'))
55 for prefix, event, value in parser:
56 if (prefix, event) == ('earth', 'map_key'):
57 stream.write('<%s>' % value)
59 elif prefix.endswith('.name'):
60 stream.write('<object name="%s"/>' % value)
61 elif (prefix, event) == ('earth.%s' % continent, 'end_map'):
62 stream.write('</%s>' % continent)
63 stream.write('</geo>')
69 Ijson provides several implementations of the actual parsing in the form of
70 backends located in ijson/backends:
72 - ``yajl2``: wrapper around `YAJL <http://lloyd.github.com/yajl/>`_ version 2.x
73 - ``yajl``: wrapper around `YAJL <http://lloyd.github.com/yajl/>`_ version 1.x
74 - ``python``: pure Python parser (good to use under PyPy)
76 You can import a specific backend and use it in the same way as the top level
79 import ijson.backends.python as ijson
81 for item in ijson.items(...):
84 Importing the top level library as ``import ijson`` tries to import all backends
85 in order, so it either finds an appropriate version of YAJL or falls back to the
86 Python backend if none is found.
92 Python parser in ijson is relatively simple thanks to `Douglas Crockford
93 <http://www.crockford.com/>`_ who invented a strict, easy to parse syntax.
95 The `YAJL <http://lloyd.github.com/yajl/>`_ library by `Lloyd Hilaiel
96 <http://lloyd.io/>`_ is the most popular and efficient way to parse JSON in an
99 Ijson was inspired by `yajl-py <http://pykler.github.com/yajl-py/>`_ wrapper by
100 `Hatem Nassrat <http://www.nassrat.ca/>`_. Though ijson borrows almost nothing
101 from the actual yajl-py code it was used as an example of integration with yajl
105 Classifier: Development Status :: 5 - Production/Stable
106 Classifier: License :: OSI Approved :: BSD License
107 Classifier: Programming Language :: Python :: 2
108 Classifier: Programming Language :: Python :: 3
109 Classifier: Topic :: Software Development :: Libraries :: Python Modules