flow php

Example: Jsonl

Topic: Data writing

Description

Write data to a json lines formatted file. Each row will become a vald JSON object.

composer.json
{
    "name": "flow-php/examples",
    "description": "Flow PHP - Examples",
    "license": "MIT",
    "type": "library",
    "require": {
        "flow-php/etl": "1.x-dev",
        "flow-php/etl-adapter-json": "1.x-dev"
    }
}
code.php
<?php

declare(strict_types=1);

use function Flow\ETL\Adapter\JSON\{to_json_lines};
use function Flow\ETL\DSL\{data_frame, from_array, overwrite};

require __DIR__ . '/vendor/autoload.php';

data_frame()
    ->read(
        from_array([
            ['id' => 1, 'name' => 'John', 'age' => 30],
            ['id' => 2, 'name' => 'Jane', 'age' => 25],
            ['id' => 3, 'name' => 'Bob', 'age' => 35],
            ['id' => 4, 'name' => 'Alice', 'age' => 28],
            ['id' => 5, 'name' => 'Charlie', 'age' => 32],
        ])
    )
    ->collect()
    ->mode(overwrite())
    ->write(to_json_lines(__DIR__ . '/output.json'))
    ->run();

Output

{"id":1,"name":"John","age":30}
{"id":2,"name":"Jane","age":25}
{"id":3,"name":"Bob","age":35}
{"id":4,"name":"Alice","age":28}
{"id":5,"name":"Charlie","age":32}

Contributors

Join us on GitHub external resource
scroll back to top