Background Information
- Module Version:
5.5.8
- Node/Browser Version:
20.0.9
The issue I'm reporting is with:
I have...
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
Background Information
5.5.820.0.9The issue I'm reporting is with:
I have...
Expected Behavior
When using
expandArrayObjects: trueandunwindArrays: true, all existent arrays should beexpandedandunwonded.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
Actual CSV✅
Code Example
Example 2 ❌
Data Sample
JSON:
[ { "Countries": [ { "Cities": [ { "Streets": [ { "Name": "Road 1", "Number": 1 }, { "Name": "Road 2", "Number": 2 } ] } ] } ] } ]Expected CSV
Actual CSV❌
Code Example
Example 3 ❌
Data Sample
JSON:
[ { "Continents": [ { "Countries": [ { "Cities": [ { "Streets": [ { "Name": "Road 1", "Number": 1 }, { "Name": "Road 2", "Number": 2 } ] }, { "field": "value" } ] } ] } ] } ]Expected CSV
Actual CSV❌
Code Example
Note
I have the fix for this, I will open a PR soon