nested-json-flattener

A php package to flatten nested json objects and nested arrays (Based on csvwriter npm package implementation)

Github星跟踪图

NestedJsonFlattener

Code Climate Build Status
codecov
Latest Stable Version Total Downloads Latest Unstable Version License

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

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.

  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.

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

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

主要指标

概览
名称与所有者tonirilix/nested-json-flattener
主编程语言PHP
编程语言PHP (语言数: 1)
平台
许可证MIT License
所有者活动
创建于2016-01-19 12:15:05
推送于2024-01-11 08:43:37
最后一次提交2018-09-05 15:58:52
发布数11
最新版本名称v2.1.2 (发布于 )
第一版名称cvswriter_v1.0.0 (发布于 2016-01-19 14:02:26)
用户参与
星数26
关注者数4
派生数12
提交数92
已启用问题?
问题数13
打开的问题数10
拉请求数8
打开的拉请求数1
关闭的拉请求数0
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?