Skip to content

Commit ce3890d

Browse files
committed
Merge pull request #36 from litixsoft/arrays_with_objects
convert objects in arrays using JSON.stringify()
2 parents 3180ddb + b3967e0 commit ce3890d

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
@@ -119,7 +119,16 @@ var convertData = function (data, keys) {
119119
*/
120120
var convertField = function (value) {
121121
if (_.isArray(value)) { // We have an array of values
122-
return options.DELIMITER.WRAP + '[' + value.join(options.DELIMITER.ARRAY) + ']' + options.DELIMITER.WRAP;
122+
var result = [];
123+
value.forEach(function(item) {
124+
if (_.isObject(item)) {
125+
// use JSON stringify to convert objects in arrays, otherwise toString() will just return [object Object]
126+
result.push(JSON.stringify(item));
127+
} else {
128+
result.push(item);
129+
}
130+
});
131+
return options.DELIMITER.WRAP + '[' + result.join(options.DELIMITER.ARRAY) + ']' + options.DELIMITER.WRAP;
123132
} else if (_.isDate(value)) { // If we have a date
124133
return options.DELIMITER.WRAP + value.toString() + options.DELIMITER.WRAP;
125134
} 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
@@ -106,6 +106,16 @@ var json2csvTests = function () {
106106
});
107107
});
108108

109+
it('should parse an array of JSON documents which includes arrays with objects to CSV', function (done) {
110+
converter.json2csv(jsonTestData.arrayValueDocsWithObjects, function (err, csv) {
111+
if (err) { throw err; }
112+
true.should.equal(_.isEqual(err, null));
113+
csv.should.equal(csvTestData.unQuoted.arrayValueDocsWithObjects);
114+
csv.split(options.EOL).length.should.equal(5);
115+
done();
116+
});
117+
});
118+
109119
it('should parse an array of JSON documents with the same schema but different ordering of fields', function (done) {
110120
converter.json2csv(jsonTestData.sameSchemaDifferentOrdering, function (err, csv) {
111121
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)