flow php

Example: Json

Topic: Data reading


Description

Read data from a json file.

function from_json(string|Path $path);

Additional options:

  • withPointer(string $pointer) - default null, used to iterate only results of a subtree, read more about pointers
  • withSchema(Schema $schema) - the schema of the dataset, when not set, it will be auto-detected

Code

<?php

declare(strict_types=1);

use function Flow\ETL\Adapter\JSON\from_json;
use function Flow\ETL\DSL\{bool_schema, data_frame, int_schema, schema, str_schema, to_stream};

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

$schema = schema(
    int_schema('id'),
    str_schema('name'),
    str_schema('email'),
    bool_schema('active'),
);

data_frame()
    ->read(
        from_json(__DIR__ . '/input/dataset.json')
            ->withSchema($schema)
    )
    ->collect()
    ->write(to_stream(__DIR__ . '/output.txt', truncate: false))
    ->run();

Output

+----+--------+------------------+--------+
| id |   name |            email | active |
+----+--------+------------------+--------+
|  1 |   John |   [email protected] |   true |
|  2 |   Paul |   [email protected] |   true |
|  3 | George | [email protected] |  false |
|  4 |  Ringo |   [email protected] |   true |
+----+--------+------------------+--------+
4 rows

Contributors

Join us on GitHub external resource
scroll back to top