etl_lib.data_source.JSONBatchSource module
- class JSONBatchSource(context, task, json_file)[source]
Bases:
BatchProcessorBatchProcessor that reads a JSON file in batches.
Two layouts are accepted and detected from the file content: JSON Lines, one object per line, and a single JSON array of objects. JSON Lines needs nothing beyond the standard library. Arrays are streamed with ijson, so the file is not loaded whole, which costs a dependency (
pip install "neo4j-etl-lib[json]").Every record gains an additional
_rowfield holding the source row number, starting at 0. The file may be gzipped.- __init__(context, task, json_file)[source]
Constructs a new JSONBatchSource.
- Parameters:
context –
etl_lib.core.ETLContext.ETLContextinstance.task (
Task|None) –etl_lib.core.Task.Taskinstance owning this processor. Pass None if the source is used outside a task, for example in a test.json_file (
Path) – Path to the JSON file. Gzipped files are detected by the.gzsuffix.
- Raises:
ImportError – If the file holds a JSON array and ijson is not installed.
- get_batch(max_batch_size)[source]
Provides a batch of data to the caller.
The batch itself could be called and processed from the provided predecessor or generated from other sources.
- Parameters:
max_batch_size (
int) – The max size of the batch the caller expects to receive.- Return type:
- Returns
A generator that yields batches.