2017 © Pedro Peláez
 

library 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.

image

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.

  • Sunday, April 3, 2016
  • by tonirilix
  • Repository
  • 4 Watchers
  • 6 Stars
  • 1,473 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 6 Open issues
  • 8 Versions
  • 30 % Grown

The README.md

NestedJsonFlattener

Code Climate Build Status codecov Latest Stable Version Total Downloads Latest Unstable Version License, (*1)

A php package to flatten nested json objects and nested arrays. It also allows you to create csv files from the flattened data., (*2)

Features

  1. 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., (*3)

  2. 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., (*4)

{
    "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:, (*5)

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., (*6)

How to use it

If you need to flat a nested json string, (*7)

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, (*8)

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, (*9)

Read JsonPath documentation from Stefan Goessner to learn how to create paths., (*10)

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, (*11)

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();

TODO

  1. The package still needs to get configurations from params. See milestone
  2. Some of the params in mind are: whether take primitives arrays as one element or not (taken as one element by default)
  3. Add a way to create a configuration to tell the class how to handle internal collections. See milestone

The Versions

03/04 2016

dev-master

9999999-dev

A php package to flatten nested json objects and nested arrays. It also allows you to create csv files from the flattened data.

  Sources   Download

MIT

The Development Requires

by Avatar tonirilix

csv json csv writer nested json convert json to csv flatten nested json json flattener

03/04 2016

dev-develop

dev-develop

A php package to flatten nested json objects and nested arrays. It also allows you to create csv files from the flattened data.

  Sources   Download

MIT

The Development Requires

by Avatar tonirilix

csv json csv writer nested json convert json to csv flatten nested json json flattener

21/01 2016

v2.1.1

2.1.1.0

A php package to flatten nested json objects and nested arrays. It also allows you to create csv files from the flattened data.

  Sources   Download

MIT

The Development Requires

by Avatar tonirilix

csv json csv writer nested json convert json to csv flatten nested json json flattener

20/01 2016

v1.1.2

1.1.2.0

A package to create cvs files from nested json objects

  Sources   Download

MIT

The Development Requires

by Avatar tonirilix

csv json csv writer nested json convert json to csv

20/01 2016

v1.1.1

1.1.1.0

A package to create cvs files from nested json objects

  Sources   Download

MIT

The Requires

 

by Avatar tonirilix

csv json csv writer nested json convert json to csv

20/01 2016

v1.1.0

1.1.0.0

A package to create cvs files from nested json objects

  Sources   Download

MIT

The Requires

 

by Avatar tonirilix

csv json csv writer nested json convert json to csv

20/01 2016

v1.0.3

1.0.3.0

A package to create cvs files from nested json objects

  Sources   Download

MIT

by Avatar tonirilix

csv json csv writer nested json convert json to csv

19/01 2016

v1.0.2

1.0.2.0

A package to create cvs files from nested json objects

  Sources   Download

MIT

by Avatar tonirilix

csv json csv writer nested json convert json to csv