Skip to content

Commit c8aae63

Browse files
authored
Merge pull request #152 from mrodrig/fix-151
Fix #151
2 parents 84c13aa + a59df3b commit c8aae63

File tree

8 files changed

+33
-3
lines changed

8 files changed

+33
-3
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
},
66
"name": "json-2-csv",
77
"description": "A JSON to CSV and CSV to JSON converter that natively supports sub-documents and auto-generates the CSV heading.",
8-
"version": "3.7.1",
8+
"version": "3.7.2",
99
"repository": {
1010
"type": "git",
1111
"url": "https://github.com/mrodrig/json-2-csv.git"

src/utils.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,15 @@ function unwindItem(accumulator, item, fieldPath) {
216216
const valueToUnwind = path.evaluatePath(item, fieldPath);
217217
let cloned = deepCopy(item);
218218

219-
if (Array.isArray(valueToUnwind)) {
219+
if (Array.isArray(valueToUnwind) && valueToUnwind.length) {
220220
valueToUnwind.forEach((val) => {
221221
cloned = deepCopy(item);
222222
accumulator.push(path.setPath(cloned, fieldPath, val));
223223
});
224+
} else if (Array.isArray(valueToUnwind) && valueToUnwind.length === 0) {
225+
// Push an empty string so the value is empty since there are no values
226+
path.setPath(cloned, fieldPath, '');
227+
accumulator.push(cloned);
224228
} else {
225229
accumulator.push(cloned);
226230
}

test/config/testCsvFilesList.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const fs = require('fs'),
3030
{key: 'quotedEmptyFieldValue', file: '../data/csv/quotedEmptyFieldValue.csv'},
3131
{key: 'csvEmptyLastValue', file: '../data/csv/csvEmptyLastValue.csv'},
3232
{key: 'unwind', file: '../data/csv/unwind.csv'},
33+
{key: 'unwindEmptyArray', file: '../data/csv/unwindEmptyArray.csv'},
3334
{key: 'unwindWithSpecifiedKeys', file: '../data/csv/unwindWithSpecifiedKeys.csv'},
3435
{key: 'localeFormat', file: '../data/csv/localeFormat.csv'},
3536
{key: 'invalidParsedValues', file: '../data/csv/invalidParsedValues.csv'}

test/config/testJsonFilesList.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ module.exports = {
2323
quotedEmptyFieldValue: require('../data/json/quotedEmptyFieldValue'),
2424
csvEmptyLastValue: require('../data/json/csvEmptyLastValue'),
2525
unwind: require('../data/json/unwind'),
26+
unwindEmptyArray: require('../data/json/unwindEmptyArray'),
2627
localeFormat: require('../data/json/localeFormat'),
2728
invalidParsedValues: require('../data/json/invalidParsedValues')
2829
};

test/data/csv/unwindEmptyArray.csv

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
_id.$oid,data.category,data.options
2+
5cf7ca3616c91100018844af,Computers,
3+
5cf7ca3616c91100018844bf,Cars,Supercharger
4+
5cf7ca3616c91100018844bf,Cars,Turbocharger
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[
2+
{
3+
"_id": {"$oid": "5cf7ca3616c91100018844af"},
4+
"data": {"category": "Computers", "options": []}
5+
},
6+
{
7+
"_id": {"$oid": "5cf7ca3616c91100018844bf"},
8+
"data": {"category": "Cars", "options": ["Supercharger", "Turbocharger"]}
9+
}
10+
]

test/json2csv.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,16 @@ function runTests(jsonTestData, csvTestData) {
415415
});
416416
});
417417

418+
it('should handle unwinding empty array values when specified', (done) => {
419+
converter.json2csv(jsonTestData.unwindEmptyArray, (err, csv) => {
420+
if (err) done(err);
421+
csv.should.equal(csvTestData.unwindEmptyArray);
422+
done();
423+
}, {
424+
unwindArrays: true
425+
});
426+
});
427+
418428
it('should unwind array values when specified', (done) => {
419429
converter.json2csv(jsonTestData.unwind, (err, csv) => {
420430
if (err) done(err);

0 commit comments

Comments
 (0)