Stream JSON parser with iterator interface
Likely abandoned. No release or commit for years (last release over 4 years ago).
Consider insteadjson-streamPythie’s Index: 681.3M downloads/moCompare →
pip install jsonslicerjsonslicer - stream JSON parser
Overview
JsonSlicer performs a stream or iterative, pull JSON parsing, which means it does not load whole JSON into memory and is able to parse very large JSON files or streams. The module is written in C and uses YAJL JSON parsing library, so it's also quite fast.
JsonSlicer takes a path of JSON map keys or array indexes, and provides iterator interface which yields JSON data matching given path as complete Python objects.
Example
{
"friends": [
{"name": "John", "age": 31},
{"name": "Ivan", "age": 26}
],
"colleagues": {
"manager": {"name": "Jack", "age": 33},
"subordinate": {"name": "Lucy", "age": 21}
}
}
from jsonslicer import JsonSlicer
# Extract specific elements:
with open('people.json') as data:
ivans_age = next(JsonSlicer(data, ('friends', 1, 'age')))
# 26
with open('people.json') as data:
managers_name = next(JsonSlicer(data, ('colleagues', 'manager', 'name')))
# 'Jack'
# Iterate over collection(s) by using wildcards in the path:
with open('people.json') as data:
for person in JsonSlicer(data, ('friends', None)):
print(person)
# {'name': 'John', 'age': 31}
# {'name': 'Ivan', 'age': 26}
# Iteration over both arrays and dicts is possible, even at the same time
with open('people.json') as data:
for person in JsonSlicer(data, (None, None)):
print(person)
# {'name': 'John', 'age': 31}
# {'name': 'Ivan', 'age': 26}
# {'name': 'Jack', 'age': 33}
# {'name': 'Lucy', 'age': 21}
# Map key of returned objects is available on demand...
with open('people.json') as data:
for position, person in JsonSlicer(data, ('colleagues', None), path_mode='map_keys'):
print(position, person)
# 'manager' {'name': 'Jack', 'age': 33}
# 'subordinate' {'name': 'Lucy', 'age': 21}
# ...as well as complete path information
with open('people.json') as data:
for *path, person in JsonSlicer(data, (None, None), path_mode='full'):
print(path, person)
# ('friends', 0) {'name': 'John', 'age': 31})
# ('friends', 1) {'name': 'Ivan', 'age': 26})
# ('colleagues', 'manager') {'name': 'Jack', 'age': 33})
# ('colleagues', 'subordinate') {'name': 'Lucy', 'age': 21})
# Extract all instances of deep nested field
with open('people.json') as data:
age_sum = sum(JsonSlicer(data, (None, None, 'age')))
# 111