Closed
Description
Background Information
- Module Version:
5.5.8
- Node/Browser Version:
20.0.9
The issue I'm reporting is with:
- json2csv
- csv2json
I have...
- searched to see if an issue has already been reported.
- verified that my JSON/CSV data is valid (using something like http://jsonlint.com or https://csvlint.io/).
- tried upgrading to the latest version of json-2-csv (since the issue may already be fixed).
Expected Behavior
When using expandArrayObjects: true
and unwindArrays: true
, all existent arrays should be expanded
and unwonded
.
Actual Behavior
From 3+ levels deep it does not always unwind or even expand all the arrays.
Example 1 ✅
Data Sample
JSON:
[
{
"Countries": [
{
"Cities": [
{
"Streets": [
{
"Name": "Road 1",
"Number": 1
},
{
"Name": "Road 2",
"Number": 2
}
]
},
{
"field": "value"
}
]
}
]
}
]
Expected CSV
Countries.Cities.Streets.Name | Countries.Cities.Streets.Number | Countries.Cities.field |
---|---|---|
Road 1 | 1 | --- |
Road 2 | 2 | --- |
--- | --- | value |
Actual CSV✅
Countries.Cities.Streets.Name | Countries.Cities.Streets.Number | Countries.Cities.field |
---|---|---|
Road 1 | 1 | --- |
Road 2 | 2 | --- |
--- | --- | value |
Code Example
// index.mjs
import { json2csv } from 'json-2-csv';
const options = {
expandArrayObjects: true,
unwindArrays: true,
emptyFieldValue: '---' // just for the sake of readability
};
const json = [{"Countries":[{"Cities":[{"Streets":[{"Name":"Road 1","Number":1},{"Name":"Road 2","Number":2}]},{"field":"value"}]}]}];
const csv = json2csv(json, options);
console.log(csv);
Example 2 ❌
Data Sample
JSON:
[
{
"Countries": [
{
"Cities": [
{
"Streets": [
{
"Name": "Road 1",
"Number": 1
},
{
"Name": "Road 2",
"Number": 2
}
]
}
]
}
]
}
]
Expected CSV
Countries.Cities.Streets.Name | Countries.Cities.Streets.Number |
---|---|
Road 1 | 1 |
Road 2 | 2 |
Actual CSV❌
Countries.Cities.Streets |
---|
[{"Name":"Road 1","Number":1},{"Name":"Road 2","Number":2}] |
Code Example
// index.mjs
import { json2csv } from 'json-2-csv';
const options = {
expandArrayObjects: true,
unwindArrays: true,
emptyFieldValue: '---' // just for the sake of readability
};
const json = [{"Countries":[{"Cities":[{"Streets":[{"Name":"Road 1","Number":1},{"Name":"Road 2","Number":2}]}]}]}];
const csv = json2csv(json, options);
console.log(csv);
Example 3 ❌
Data Sample
JSON:
[
{
"Continents": [
{
"Countries": [
{
"Cities": [
{
"Streets": [
{
"Name": "Road 1",
"Number": 1
},
{
"Name": "Road 2",
"Number": 2
}
]
},
{
"field": "value"
}
]
}
]
}
]
}
]
Expected CSV
Continents.Countries.Cities.Streets.Name | Continents.Countries.Cities.Streets.Number | Continents.Countries.Cities.field |
---|---|---|
Road 1 | 1 | --- |
Road 2 | 2 | --- |
--- | --- | value |
Actual CSV❌
Continents.Countries.Cities |
---|
[{"Streets":[{"Name":"Road 1","Number":1},{"Name":"Road 2","Number":2}]},{"field":"value"}] |
Code Example
// index.mjs
import { json2csv } from 'json-2-csv';
const options = {
expandArrayObjects: true,
unwindArrays: true,
emptyFieldValue: '---' // just for the sake of readability
};
const json = [{"Continents":[{"Countries":[{"Cities":[{"Streets":[{"Name":"Road 1","Number":1},{"Name":"Road 2","Number":2}]},{"field":"value"}]}]}]}];
const csv = json2csv(json, options);
console.log(csv);
Note
I have the fix for this, I will open a PR soon