dilawar/nested-json-flattener
最新稳定版本:v3.0.1
Composer 安装命令:
composer require 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.
README 文档
README
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
-
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(); 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
dilawar/nested-json-flattener 适用场景与选型建议
dilawar/nested-json-flattener 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.55k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「json」 「csv」 「csv writer」 「nested json」 「convert json to csv」 「Flatten nested json」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 dilawar/nested-json-flattener 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dilawar/nested-json-flattener 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 dilawar/nested-json-flattener 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Kinikit - PHP Application development framework MVC component
Simple RSS generator library for PHP 5.5 or later. clone from Bhaktaraz Bhatta ttps://github.com/bhaktaraz/php-rss-generator
ext-json wrapper with sane defaults
A package to cast json fields, each sub-keys is castable
laravel facade to read/write csv file
swagger-php - Generate interactive documentation for your RESTful API using phpdoc annotations
统计信息
- 总下载量: 1.55k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 21
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-04