flow-php/etl-adapter-json
Laravel-friendly adapter for Flow PHP ETL that reads and writes JSON data, enabling JSON files or streams to be used as ETL sources and destinations. Simple integration with pipelines for transforming and loading structured data.
Installation
composer require flow-php/etl-adapter-json
Ensure your project uses Flow Framework (v10+).
Basic Usage Import the adapter in your ETL pipeline:
use Flow\ETL\Adapter\JsonAdapter;
use Flow\ETL\Pipeline;
$pipeline = new Pipeline();
$pipeline->addAdapter(new JsonAdapter());
First Use Case: Parsing JSON Input
$jsonInput = '{"name": "John", "age": 30}';
$result = $pipeline->process($jsonInput);
// Result: Associative array `['name' => 'John', 'age' => 30]`
Key Files
src/Adapter/JsonAdapter.php (Core logic)tests/ (Unit/integration tests for reference)$pipeline
->addAdapter(new JsonAdapter()) // Parse JSON
->addAdapter(new \Flow\ETL\Adapter\FilterAdapter()) // Filter data
->addAdapter(new \Flow\ETL\Adapter\ArrayAdapter()); // Output as array
JsonAdapter as the first step in a pipeline to parse raw JSON input.TryCatchAdapter for graceful JSON decode failures:
$pipeline
->addAdapter(new TryCatchAdapter(new JsonAdapter()))
->addAdapter(new LogAdapter()); // Log errors
JsonAdapter to handle non-standard JSON:
class CustomJsonAdapter extends JsonAdapter {
protected function decode(string $json): array {
return json_decode($json, true, 512, JSON_BIGINT_AS_STRING);
}
}
JsonAdapter with StreamAdapter for large files:
$pipeline
->addAdapter(new StreamAdapter())
->addAdapter(new JsonAdapter());
Malformed JSON
JsonAdapter throws \JsonException on invalid JSON. Use TryCatchAdapter or validate input first:
if (!json_validate($json)) { /* handle error */ }
Associative Array Assumption
json_decode($json, true) always returns an array, even for objects. Use JsonObjectAdapter (if available) for object preservation.Memory Limits
memory_limit. Use JSON_BIGINT_AS_STRING or chunk processing.JsonAdapter::setThrowExceptions(false); // Default: true
$pipeline->addAdapter(new LogAdapter()); // Log decode errors
JsonAdapter has no config options. Customize via subclassing or wrapper adapters.json_decode() defaults (associative arrays, no bigint handling).Custom Decoders
Override decode() to support custom JSON formats (e.g., JSON5, JSON Lines):
class JsonLinesAdapter extends JsonAdapter {
protected function decode(string $json): array {
return array_map('json_decode', explode("\n", $json));
}
}
Pre/Post-Processing
Use BeforeProcessAdapter/AfterProcessAdapter to transform data before/after JSON parsing:
$pipeline
->addAdapter(new BeforeProcessAdapter(function($data) {
return str_replace('"', "'", $data); // Pre-process
}))
->addAdapter(new JsonAdapter());
Performance
AfterProcessAdapter if reprocessing identical inputs:
$cache = [];
$pipeline->addAdapter(new AfterProcessAdapter(function($data) use (&$cache) {
$cache[md5($data)] = $data;
return $data;
}));
How can I help you explore Laravel packages today?