zemirco / json2csv

Convert json to csv with column titles
http://zemirco.github.io/json2csv
MIT License
2.71k stars 365 forks source link

unwindPath only accepts one field #174

Closed apat183 closed 7 years ago

apat183 commented 7 years ago

More of a questions then issue, does unwindPath option only accept one field?

I've tried to put an array and string array into option but doesn't unwind fields. If I only put one field in then it works. Any idea how you can unwind multiple fields?

Thanks

knownasilya commented 7 years ago

Confirmed, it only takes one argument. You could implement your own unwind and pass the resulting data to this module.

We also accept pull requests 👍

apat183 commented 7 years ago

Thanks for confirmation, since I'm passing results from MongoDB it will be easier to unwind myself and pass through.

eduardomourar commented 7 years ago

@knownasilya and others, I was looking for this exact functionality. If I have this JSON as an example:

var myCars = [
    {
        "carModel": "BMW",
        "price": 15000,
        "items": [
            {
                "name": "airbag",
                "color": "white"
            }, {
                "name": "dashboard",
                "color": "black"
            },
        ]
    }, {
        "carModel": "Porsche",
        "price": 30000,
        "items": [
            {
                "name": "dashboard",
                "items": [
                    {
                        "position": "left",
                        "color": "black"
                    }, {
                        "position": "right",
                        "color": "gray"
                    }
                ]
            }
        ]
    }
];

I want to have following output CSV:

"BMW",15000,"airbag","white",,
"BMW",15000,"dashboard","black",,
"Porsche",30000,"dashboard",,"left","black"
"Porsche",30000,"dashboard",,"right"","gray"

I can submit a PR, if everyone agrees with these changes to parameters:

var csv = json2csv({
    data: myCars,
    fields: ['carModel','price','items.name','items.color','items.items.position','items.items.color'],
    unwindPath: ['items','items.items']
});
apat183 commented 7 years ago

Hi ended up unwinding in mongodb first then passing the results but would love to have it in json2csv as well but it's up to the moderators to agree on parameters etc.

eduardomourar commented 7 years ago

I simplified a little bit the parameters and created a pull request for that.