etl_lib.data_source.JSONBatchSource module

class JSONBatchSource(context, task, json_file)[source]

Bases: BatchProcessor

BatchProcessor 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 _row field holding the source row number, starting at 0. The file may be gzipped.

Parameters:
__init__(context, task, json_file)[source]

Constructs a new JSONBatchSource.

Parameters:
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:

Generator[BatchResults, None, None]

Returns

A generator that yields batches.