Iterative JSON parser with a standard Python iterator interface
Project description
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ijson-2.2.tar.gz.
File metadata
- Download URL: ijson-2.2.tar.gz
- Upload date:
- Size: 10.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
874b746c78279a9645a5d2078667775b12b317b0d784ba887a881348c2ac357d
|
|
| MD5 |
48c85856c04895bf6d9c4f85be87c691
|
|
| BLAKE2b-256 |
d5fa1e50340b34cae75f14300affd65d359a2fdb1fca64b341665bb87eedcc4e
|
File details
Details for the file ijson-2.2-py2.py3-none-any.whl.
File metadata
- Download URL: ijson-2.2-py2.py3-none-any.whl
- Upload date:
- Size: 14.7 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1af885ed5b8ccf7f281d04c7b608a3af8f88e6f121cce718358eef3a9dc9dac3
|
|
| MD5 |
5c08f7b135ea1c6ab8369bcc057c7b31
|
|
| BLAKE2b-256 |
013e4442678be22cc1bad35ae89bd4eb27c1dcbfc97581416b2f9308570e248c
|