Skip to content

Commit b3967e0

Browse files
author
Andreas Krummsdorf
committed
convert objects in arrays using JSON.stringify()
1 parent 745f7c5 commit b3967e0

File tree

6 files changed

+49
-1
lines changed

6 files changed

+49
-1
lines changed

lib/json-2-csv.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,16 @@ var convertData = function (data, keys) {
108108
*/
109109
var convertField = function (value) {
110110
if (_.isArray(value)) { // We have an array of values
111-
return options.DELIMITER.WRAP + '[' + value.join(options.DELIMITER.ARRAY) + ']' + options.DELIMITER.WRAP;
111+
var result = [];
112+
value.forEach(function(item) {
113+
if (_.isObject(item)) {
114+
// use JSON stringify to convert objects in arrays, otherwise toString() will just return [object Object]
115+
result.push(JSON.stringify(item));
116+
} else {
117+
result.push(item);
118+
}
119+
});
120+
return options.DELIMITER.WRAP + '[' + result.join(options.DELIMITER.ARRAY) + ']' + options.DELIMITER.WRAP;
112121
} else if (_.isDate(value)) { // If we have a date
113122
return options.DELIMITER.WRAP + value.toString() + options.DELIMITER.WRAP;
114123
} else if (_.isObject(value)) { // If we have an object
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
info.name,coursesTaken,year
2+
Mike,[CS2500;CS2510],Sophomore
3+
John,[ANTH1101;POL2312;MATH2142;POL3305;LAW2100],Senior
4+
Joe,[{"name":"CS2500","students":100};{"name":"CS2510","students":78}],Freshman
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[
2+
{
3+
"info": {
4+
"name": "Mike"
5+
},
6+
"coursesTaken": ["CS2500", "CS2510"],
7+
"year": "Sophomore"
8+
},
9+
{
10+
"info": {
11+
"name": "John"
12+
},
13+
"coursesTaken": ["ANTH1101", "POL2312", "MATH2142", "POL3305", "LAW2100"],
14+
"year": "Senior"
15+
},
16+
{
17+
"info": {
18+
"name": "Joe"
19+
},
20+
"coursesTaken": [{"name": "CS2500", "students": 100}, {"name": "CS2510", "students": 78}],
21+
"year": "Freshman"
22+
}
23+
]

test/testCsvFilesList.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"key": "unQuoted",
44
"files": [
55
{"key": "arrayValue", "file": "test/CSV/unQuoted/arrayValueDocs.csv"},
6+
{"key": "arrayValueDocsWithObjects", "file": "test/CSV/unQuoted/arrayValueDocsWithObjects.csv"},
67
{"key": "arrayValue_specificKeys", "file": "test/CSV/unQuoted/arrayValueDocs_specificKeys.csv"},
78
{"key": "nestedJson", "file": "test/CSV/unQuoted/nestedJson.csv"},
89
{"key": "nestedJson2", "file": "test/CSV/unQuoted/nestedJson2.csv"},

test/testJson2Csv.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,16 @@ var json2csvTests = function () {
8686
});
8787
});
8888

89+
it('should parse an array of JSON documents which includes arrays with objects to CSV', function (done) {
90+
converter.json2csv(jsonTestData.arrayValueDocsWithObjects, function (err, csv) {
91+
if (err) { throw err; }
92+
true.should.equal(_.isEqual(err, null));
93+
csv.should.equal(csvTestData.unQuoted.arrayValueDocsWithObjects);
94+
csv.split(options.EOL).length.should.equal(5);
95+
done();
96+
});
97+
});
98+
8999
it('should parse an array of JSON documents with the same schema but different ordering of fields', function (done) {
90100
converter.json2csv(jsonTestData.sameSchemaDifferentOrdering, function (err, csv) {
91101
if (err) { throw err; }

test/testJsonFilesList.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module.exports = {
22
arrayValue: require('./JSON/arrayValueDocs'),
3+
arrayValueDocsWithObjects: require('./JSON/arrayValueDocsWithObjects'),
34
arrayValue_specificKeys: require('./JSON/arrayValueDocs_specificKeys'),
45
nestedComma: require('./JSON/nestedComma'),
56
nestedJson: require('./JSON/nestedJson'),

0 commit comments

Comments
 (0)