tonirilix / nested-json-flattener
A php package to flatten nested json objects and nested arrays. It also allows you to create csv files from the flattened data.
Installs: 102 813
Dependents: 1
Suggesters: 0
Security: 0
Stars: 27
Watchers: 4
Forks: 12
Open Issues: 10
pkg:composer/tonirilix/nested-json-flattener
Requires
- php: ^7.1 || ^8.0
- peekmo/jsonpath: dev-master
Requires (Dev)
- doctrine/instantiator: ^1.5
- phpunit/phpunit: ^7.5 || ^8.5 || ^9.6
This package is not auto-updated.
Last update: 2026-02-05 03:56:15 UTC
README
A php package to flatten nested json objects and nested arrays. It also allows you to create csv files from the flattened data.
Features
-
The package allows you to select a specific node of the json object or array and flat it. The selected node can be flattened whether is a object or collection.
-
It takes in count the full path where a value is stored in a nested json object and uses it as header name. Let's use the example below.
{
"name": "This is a name",
"nested": {
"type": "This is a type",
"location": "Earth",
"geo": {
"latitude": "1234567890",
"longitude": "0987654321"
},
"primitivesCollection":[123, 456, 789]
}
}
If we'd like to flat that json object and put it into a csv file, the result would be as follows:
| name | nested.type | nested.location | nested.geo.latitude | nested.geo.longitude | nested.primitivesCollection |
|---|---|---|---|---|---|
| This is a name | This is a type | Earth | 1234567890 | 0987654321 | 123, 456, 789 |
Credits
It's based on csvwriter npm package implementation.
How to use it
If you need to flat a nested json string
use NestedJsonFlattener\Flattener\Flattener;
$dataJson = '{
"name": "This is a name",
"nested": {
"type": "This is a type",
"location": "Earth",
"geo": {
"latitude": "1234567890",
"longitude": "0987654321"
},
"primitivesCollection":[123, 456, 789]
}
}';
$flattener = new Flattener();
$flattener->setJsonData($dataJson);
$flat = $flattener->getFlatData();
print_r($flat);
If you need to flat a nested array
use NestedJsonFlattener\Flattener\Flattener;
$data = [
'name' => 'This is a name',
'nested' => [
'type' => 'This is a type',
'location' => 'Earth',
'geo' => [
'latitude'=> '1234567890',
'longitude'=> '0987654321'
],
'primitivesCollection'=> [123, 456, 789]
]
];
$flattener = new Flattener();
$flattener->setArrayData($data);
$flat = $flattener->getFlatData();
print_r($flat);
If you need to select a specific path to be flattened
Read JsonPath documentation from Stefan Goessner to learn how to create paths.
use NestedJsonFlattener\Flattener\Flattener;
$data = [
'name' => 'This is a name',
'nested' => [
'type' => 'This is a type',
'location' => 'Earth',
'geo' => [
'latitude'=> '1234567890',
'longitude'=> '0987654321'
],
'primitivesCollection'=> [123, 456, 789]
]
];
// This is a path based on JsonPath implementation
$options = ['path'=>'$.nested'];
$flattener = new Flattener($options);
$flattener->setArrayData($data);
$flat = $flattener->getFlatData();
print_r($flat);
If you need to write a csv file
use NestedJsonFlattener\Flattener\Flattener;
$data = [
'name' => 'This is a name',
'nested' => [
'type' => 'This is a type',
'location' => 'Earth',
'geo' => [
'latitude'=> '1234567890',
'longitude'=> '0987654321'
],
'primitivesCollection'=> [123, 456, 789]
]
];
$flattener = new Flattener();
$flattener->setArrayData($data);
$flattener->writeCsv();
Local development
Requirements
- PHP 7.1+ (tested up to PHP 8.1)
- Composer
Install dependencies
composer install
Run the unit tests
composer test
Running the suite on PHP 8.x currently surfaces a few deprecation warnings from the legacy CSV writer implementation. They are harmless and documented here until the runtime is modernized.
TODO
- The package still needs to get configurations from params. See milestone
- Some of the params in mind are: whether take primitives arrays as one element or not (taken as one element by default)
- Add a way to create a configuration to tell the class how to handle internal collections. See milestone