Intercept and filter events between ijson routines
masterThe ijson routines are chained: basic_parse $\rightarrow$ parse $\rightarrow$ kvitems/items. You can intercept this chain by passing the output (an iterable of events) of one function as the input to another. This allows for custom filtering or injection before full object construction.
import io
import ijson
# Create a parser from a byte stream
parse_events = ijson.parse(io.BytesIO(b'["skip", {"a": 1}, {"b": 2}, {"c": 3}]'))
# Intercept: skip everything until we find the value 'skip'
while True:
prefix, event, value = next(parse_events)
if value == "skip":
break
# Pass the remaining events to items()
for obj in ijson.items(parse_events, 'item'):
print(obj)