Writes each received array as a JSON document on its own line (JSON Lines format), in a file whose path is set in the options. As a blocking task, it waits until all inputs have been received and then outputs the file path.
- Service:
CleverAge\ProcessBundle\Task\File\JsonStream\JsonStreamWriterTask - Code:
json_stream_writer(task: json_stream_writer) - Blocking task
array: data to encode. An UnexpectedValueException is thrown for any other type.
Underlying method is json_encode.
string: path of the written file (with placeholders replaced), once all inputs have been processed.
| Code | Type | Required | Default | Description |
|---|---|---|---|---|
file_path |
string |
X | Path of the file to write to; missing parent directories are created. Placeholders {date}, {date_time}, {timestamp} and {unique_token} are replaced in the path |
|
mode |
string |
wb |
File open mode (see fopen mode parameter): wb overwrites the file, ab appends to it |
|
spl_file_object_flags |
array|null |
null |
List of SplFileObject flags, summed and passed to SplFileObject::setFlags.null means DROP_NEW_LINE, READ_AHEAD and SKIP_EMPTY; an empty array means no flag |
|
json_flags |
array|null |
null |
List of JSON flags, summed and passed to json_encode.null means JSON_THROW_ON_ERROR; an empty array means no flag |
# Task configuration level
entry:
task: constant_iterable_output
options:
output:
- column1: value1-1
column2: value2-1
- column1: value1-2
column2: value2-2
outputs: [write]
write:
task: json_stream_writer
options:
file_path: '%kernel.project_dir%/var/data/json_stream_writer_{date_time}.json'
json_flags:
- !php/const JSON_THROW_ON_ERROR
- !php/const JSON_UNESCAPED_SLASHES
- !php/const JSON_UNESCAPED_UNICODE{date}is replaced byYmd,{date_time}byYmd_His,{timestamp}by the Unix timestamp and{unique_token}by auniqid('', true)value (23 characters, containing a dot).- The parent directory of the file is created (recursively) if it does not exist.
- Using
JSON_PRETTY_PRINTproduces multi-line documents: the file can no longer be read by JsonStreamReaderTask. - The file is released at the end of the process.