diff --git a/src/json2csv.js b/src/json2csv.js index 08c0dd4..8785023 100755 --- a/src/json2csv.js +++ b/src/json2csv.js @@ -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; @@ -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; diff --git a/test/config/testCsvFilesList.js b/test/config/testCsvFilesList.js index 87fd153..3c6c5fe 100644 --- a/test/config/testCsvFilesList.js +++ b/test/config/testCsvFilesList.js @@ -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'}, diff --git a/test/config/testJsonFilesList.js b/test/config/testJsonFilesList.js index 49308bf..7a577b6 100644 --- a/test/config/testJsonFilesList.js +++ b/test/config/testJsonFilesList.js @@ -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'), diff --git a/test/data/csv/arraySingleArray.csv b/test/data/csv/arraySingleArray.csv new file mode 100644 index 0000000..a41adb4 --- /dev/null +++ b/test/data/csv/arraySingleArray.csv @@ -0,0 +1,2 @@ +list.name,list.features.name,list.features.price.currency,list.features.price.amount +Shopping List,Television,USD,100 \ No newline at end of file diff --git a/test/data/json/arraySingleArray.json b/test/data/json/arraySingleArray.json new file mode 100644 index 0000000..7bb3df3 --- /dev/null +++ b/test/data/json/arraySingleArray.json @@ -0,0 +1,16 @@ +{ + "list": [ + { + "name": "Shopping List", + "features": [ + { + "name": "Television", + "price": { + "currency": "USD", + "amount": 100 + } + } + ] + } + ] +} \ No newline at end of file diff --git a/test/json2csv.js b/test/json2csv.js index c7ceb35..4516db0 100644 --- a/test/json2csv.js +++ b/test/json2csv.js @@ -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);