Closed johanhaleby closed 4 years ago
Anything I can do to help?
Sorry for the long delay @johanhaleby! My first attempt didn't really pan out, but the approach I took works well from my testing. Here's an example of how to use the new unwindArrays
option that's added in 3.6.0
:
let converter = require('json-2-csv');
const jsonArray = [{
"_id": {"$oid": "5cf7ca3616c91100018844af"},
"data": {"category": "stuff", "items": [{"title": "title1", "description": "description1"}, {"title": "title2", "description": "description2"}]}
},
{
"_id": {"$oid": "5cf7ca3616c91100018844bf"},
"data": {"category": "stuff", "items": [{"title": "title3", "description": "description3"}, {"title": "title4", "description": "description4"}]}
},
{
"_id": {"$oid": "5cf7ca3616c91100018844cf"},
"data": {"category": "other stuff", "items": [{"title": "title5", "description": "description5"}, {"title": "title6", "description": "description6"}]}
}];
converter.json2csv(jsonArray,
(error, csv) => {
console.log(csv)
},
{
unwindArrays: true,
keys: ['data.category', 'data.items.title']
});
Which produces:
data.category,data.items.title
stuff,title1
stuff,title2
stuff,title3
stuff,title4
other stuff,title5
other stuff,title6
Background Information
^3.5.4
v12.4.0
The issue I'm reporting is with:
I have...
Expected Behavior
If I have the following code:
Then I expect the output to be:
Actual Behavior
Output is:
Data Sample
JSON:
Code Example