PHPackages                             tonirilix/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. tonirilix/nested-json-flattener

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

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.

v2.1.4(3mo ago)28107.6k↓38.9%12[9 issues](https://github.com/tonirilix/nested-json-flattener/issues)MITPHPPHP ^7.1 || ^8.0CI passing

Since Jan 19Pushed 3mo ago5 watchersCompare

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

READMEChangelog (4)Dependencies (3)Versions (11)Used By (0)

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

[](#nestedjsonflattener)

[![Code Climate](https://camo.githubusercontent.com/62bc05793b1921b3523e166be77147c0869918dc179f7b731cb11c7999706515/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f746f6e6972696c69782f6e65737465642d6a736f6e2d666c617474656e65722f6261646765732f6770612e737667)](https://codeclimate.com/github/tonirilix/nested-json-flattener) [![Build Status](https://camo.githubusercontent.com/df243166bab0dcb60909cea2af896e4c56829efb8d871cacdd461efd31ef8222/68747470733a2f2f7472617669732d63692e6f72672f746f6e6972696c69782f6e65737465642d6a736f6e2d666c617474656e65722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/tonirilix/nested-json-flattener) [![GitHub Actions](https://github.com/tonirilix/nested-json-flattener/actions/workflows/tests.yml/badge.svg?branch=master)](https://github.com/tonirilix/nested-json-flattener/actions/workflows/tests.yml)[![codecov](https://camo.githubusercontent.com/c63ea51353ef6db36e2041ccc67e226a3a23dc138d027f84e4da7636c1f95fe0/68747470733a2f2f636f6465636f762e696f2f67682f746f6e6972696c69782f6e65737465642d6a736f6e2d666c617474656e65722f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/tonirilix/nested-json-flattener)[![Latest Stable Version](https://camo.githubusercontent.com/1e612b243208ab5b9c18fbb74f144d7296cfd6dd4ca00b4b28bc585dff13fabc/68747470733a2f2f706f7365722e707567782e6f72672f746f6e6972696c69782f6e65737465642d6a736f6e2d666c617474656e65722f762f737461626c65)](https://packagist.org/packages/tonirilix/nested-json-flattener) [![Total Downloads](https://camo.githubusercontent.com/a9487cd2d01dc0fb894885f21a348663205e2e7e8f973d976902f727204198c6/68747470733a2f2f706f7365722e707567782e6f72672f746f6e6972696c69782f6e65737465642d6a736f6e2d666c617474656e65722f646f776e6c6f616473)](https://packagist.org/packages/tonirilix/nested-json-flattener) [![Latest Unstable Version](https://camo.githubusercontent.com/146858d2b235441d2b530c642acad62659db0899b77ca3bc15e19dce5c689629/68747470733a2f2f706f7365722e707567782e6f72672f746f6e6972696c69782f6e65737465642d6a736f6e2d666c617474656e65722f762f756e737461626c65)](https://packagist.org/packages/tonirilix/nested-json-flattener) [![License](https://camo.githubusercontent.com/6f5e940f1b5320c8dfc7361569774b583a8dbc94e0e6b932258ea6fcd5b0ccfe/68747470733a2f2f706f7365722e707567782e6f72672f746f6e6972696c69782f6e65737465642d6a736f6e2d666c617474656e65722f6c6963656e7365)](https://packagist.org/packages/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.

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

```

Local development
-----------------

[](#local-development)

### Requirements

[](#requirements)

- PHP 7.1+ (tested up to PHP 8.1)
- [Composer](https://getcomposer.org/)

### Install dependencies

[](#install-dependencies)

```
composer install

```

### Run the unit tests

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

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

56

—

FairBetter than 98% of packages

Maintenance74

Regular maintenance activity

Popularity44

Moderate usage in the ecosystem

Community15

Small or concentrated contributor base

Maturity74

Established project with proven stability

 Bus Factor1

Top contributor holds 99% 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 ~458 days

Recently: every ~917 days

Total

9

Last Release

103d ago

Major Versions

v1.1.2 → v2.1.12016-01-21

### Community

Maintainers

![](https://www.gravatar.com/avatar/f052ee49ef81591a279a347f694fa58d957a43f21219a26cf310fc70551f6ca7?d=identicon)[tonirilix](/maintainers/tonirilix)

---

Top Contributors

[![tonirilix](https://avatars.githubusercontent.com/u/3756508?v=4)](https://github.com/tonirilix "tonirilix (95 commits)")[![CJLees01](https://avatars.githubusercontent.com/u/4609788?v=4)](https://github.com/CJLees01 "CJLees01 (1 commits)")

---

Tags

jsoncsvcsv writernested jsonconvert json to csvFlatten nested jsonjson flattener

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[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)[ee/dataexporter-bundle

Easy export data to CSV, XML, HTML, JSON or XLS

4982.5k](/packages/ee-dataexporter-bundle)

PHPackages © 2026

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