Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/json2csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ const Json2Csv = function(options) {
* @param params {Object}
* @returns {Promise}
*/
function unwindRecordsIfNecessary(params) {
function unwindRecordsIfNecessary(params, finalPass = false) {
if (options.unwindArrays) {
const originalRecordsLength = params.records.length;

Expand All @@ -178,6 +178,12 @@ const Json2Csv = function(options) {
}
// Otherwise, we didn't unwind any additional arrays, so continue...

// Run a final time in case the earlier unwinding exposed additional
// arrays to unwind...
if (!finalPass) {
return unwindRecordsIfNecessary(params, true);
}

// If keys were provided, set the headerFields to the provided keys:
if (options.keys) {
params.headerFields = options.keys;
Expand Down
1 change: 1 addition & 0 deletions test/config/testCsvFilesList.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const fs = require('fs'),
{key: 'array', file: '../data/csv/array.csv'},
{key: 'arrayObjects', file: '../data/csv/arrayObjects.csv'},
{key: 'arrayMixedObjNonObj', file: '../data/csv/arrayMixedObjNonObj.csv'},
{key: 'arraySingleArray', file: '../data/csv/arraySingleArray.csv'},
{key: 'date', file: '../data/csv/date.csv'},
{key: 'null', file: '../data/csv/null.csv'},
{key: 'undefined', file: '../data/csv/undefined.csv'},
Expand Down
1 change: 1 addition & 0 deletions test/config/testJsonFilesList.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module.exports = {
array: require('../data/json/array'),
arrayObjects: require('../data/json/arrayObjects'),
arrayMixedObjNonObj: require('../data/json/arrayMixedObjNonObj'),
arraySingleArray: require('../data/json/arraySingleArray'),
date: require('../data/json/date'),
null: require('../data/json/null'),
undefined: require('../data/json/undefined'),
Expand Down
2 changes: 2 additions & 0 deletions test/data/csv/arraySingleArray.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
list.name,list.features.name,list.features.price.currency,list.features.price.amount
Shopping List,Television,USD,100
16 changes: 16 additions & 0 deletions test/data/json/arraySingleArray.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"list": [
{
"name": "Shopping List",
"features": [
{
"name": "Television",
"price": {
"currency": "USD",
"amount": 100
}
}
]
}
]
}
10 changes: 10 additions & 0 deletions test/json2csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,16 @@ function runTests(jsonTestData, csvTestData) {
});
});

it('should unwind nested array values when the earlier array has a length of 1', (done) => {
converter.json2csv(jsonTestData.arraySingleArray, (err, csv) => {
if (err) done(err);
csv.should.equal(csvTestData.arraySingleArray);
done();
}, {
unwindArrays: true
});
});

it('should unwind array values when specified - with keys specified', (done) => {
converter.json2csv(jsonTestData.unwind, (err, csv) => {
if (err) done(err);
Expand Down