mrodrig / json-2-csv

Convert JSON to CSV *or* CSV to JSON!
https://mrodrig.github.io/json-2-csv
MIT License
421 stars 58 forks source link

Using nested keys doesn't generate CSV correctly? #124

Closed johanhaleby closed 4 years ago

johanhaleby commented 5 years ago

Background Information

The issue I'm reporting is with:

I have...

Expected Behavior

If I have the following code:

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)
        },
        {
            expandArrayObjects: true,
            keys: ['data.category', 'data.items.title']
        });

Then I expect the output to be:

data.category,data.items.title
stuff,title1
stuff,title2
stuff,title3
stuff,title4
other stuff,title5
other stuff,title6

Actual Behavior

Output is:

data.category,data.items.title
stuff,"[""title1"",""title2""]"
stuff,"[""title3"",""title4""]"
other stuff,"[""title5"",""title6""]"

Data Sample

JSON:

[{
    "_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"}]}
    }];

Code Example

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)
        },
        {
            expandArrayObjects: true,
            keys: ['data.category', 'data.items.title']
        });
johanhaleby commented 5 years ago

Anything I can do to help?

mrodrig commented 4 years ago

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