PHPackages                             dilawar/nested-json-flattener - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [PDF &amp; Document Generation](/categories/documents)
4. /
5. dilawar/nested-json-flattener

ActiveLibrary[PDF &amp; Document Generation](/categories/documents)

dilawar/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.

v3.0.1(7mo ago)02.7k↑91.7%[1 PRs](https://github.com/dilawar/nested-json-flattener/pulls)MITPHPPHP &gt;8.0CI failing

Since Jun 12Pushed 7mo agoCompare

[ Source](https://github.com/dilawar/nested-json-flattener)[ Packagist](https://packagist.org/packages/dilawar/nested-json-flattener)[ RSS](/packages/dilawar-nested-json-flattener/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (3)Versions (4)Used By (0)

NestedJsonFlattener
===================

[](#nestedjsonflattener)

[![PHP Composer](https://github.com/dilawar/nested-json-flattener/actions/workflows/php.yml/badge.svg)](https://github.com/dilawar/nested-json-flattener/actions/workflows/php.yml)

**This fork is under development. Use it at your own risk.**

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

Features
--------

[](#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:

namenested.typenested.locationnested.geo.latitudenested.geo.longitudenested.primitivesCollectionThis is a nameThis is a typeEarth12345678900987654321123, 456, 789Credits
-------

[](#credits)

It's based on [csvwriter](https://www.npmjs.com/package/csvwriter) npm package implementation.

How to use it
-------------

[](#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](http://goessner.net/articles/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
----

[](#todo)

1. The package still needs to get configurations from params. [See milestone](https://github.com/tonirilix/nested-json-flattener/milestones/Configuration%20Options)
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](https://github.com/tonirilix/nested-json-flattener/milestones/Internal%20collections%20handler)

###  Health Score

37

—

LowBetter than 83% of packages

Maintenance64

Regular maintenance activity

Popularity22

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity44

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 84.3% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~117 days

Total

2

Last Release

223d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/35959cf75da0cd40871df6db7118628940a0dac1a35da7e31cbd33c083e3c1a9?d=identicon)[dilawar](/maintainers/dilawar)

---

Top Contributors

[![tonirilix](https://avatars.githubusercontent.com/u/3756508?v=4)](https://github.com/tonirilix "tonirilix (91 commits)")[![dilawar](https://avatars.githubusercontent.com/u/895681?v=4)](https://github.com/dilawar "dilawar (15 commits)")[![CJLees01](https://avatars.githubusercontent.com/u/4609788?v=4)](https://github.com/CJLees01 "CJLees01 (1 commits)")[![tunder](https://avatars.githubusercontent.com/u/1682645?v=4)](https://github.com/tunder "tunder (1 commits)")

---

Tags

jsoncsvcsv writernested jsonconvert json to csvFlatten nested jsonjson flattener

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/dilawar-nested-json-flattener/health.svg)

```
[![Health](https://phpackages.com/badges/dilawar-nested-json-flattener/health.svg)](https://phpackages.com/packages/dilawar-nested-json-flattener)
```

###  Alternatives

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

28107.6k](/packages/tonirilix-nested-json-flattener)[mledoze/countries

List of world countries in JSON, CSV, XML and YAML

6.2k699.7k6](/packages/mledoze-countries)[faisalman/simple-excel-php

Easily parse / convert / write between Microsoft Excel XML / CSV / TSV / HTML / JSON / etc formats

582599.4k1](/packages/faisalman-simple-excel-php)[kartik-v/yii2-export

A library to export server/db data in various formats (e.g. excel, html, pdf, csv etc.)

1623.1M35](/packages/kartik-v-yii2-export)[rodenastyle/stream-parser

PHP Multiformat Streaming Parser

443195.7k2](/packages/rodenastyle-stream-parser)[ozdemirburak/json-csv

JSON to CSV and CSV to JSON converters in PHP.

40166.5k1](/packages/ozdemirburak-json-csv)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
