Coder Social home page Coder Social logo

allow keys to be json paths about json-2-csv HOT 3 CLOSED

mrodrig avatar mrodrig commented on July 18, 2024
allow keys to be json paths

from json-2-csv.

Comments (3)

mrodrig avatar mrodrig commented on July 18, 2024

Hi @jimthedev, thanks!

I can definitely see the usefulness of supporting keys nested in arrays, which it appears the current keys option doesn't support based on my testing. Unfortunately I won't have time to look into this for a little bit (because of grad school exams coming up), but it would be a nice addition.

Just for future reference, here's what I tried with the current code:

var converter = require('json-2-csv');

var options = {
    keys : ['Make', 'Model', 'Year', 'Features.name']
};

var documents = [
    {
        Make: 'Nissan',
        Model: ' Murano ', // Note: This value has additional padding which can be trimmed
        Year: '2013',
        Features: [{ name: 'AC', value: 1}, {name: 'DVD', value: 2}]
    }
];

var json2csvCallback = function (err, csv) {
    if (err) throw err;
    console.log(csv);
};

converter.json2csv(documents, json2csvCallback, options);

from json-2-csv.

jimthedev avatar jimthedev commented on July 18, 2024

Cool. That looks similar to what I have been testing (which currently doesn't work). If I get to it I'll do a PR. Thanks for confirming.

from json-2-csv.

mrodrig avatar mrodrig commented on July 18, 2024

As of 3.2.0, this is now supported! So for the following JSON documents, you can specify {keys: ['features.name']} in your options and it will pull the correct values:

[
        {
            "name": "list",
            "features": [
                {
                    "name": "modules"
                },
                {
                    "pros": "efficiency"
                },
                {
                    "cons": ["cost", "time"]
                }
            ]
        },
        {
            "name": "module",
            "features": [
                {
                    "name": "testing"
                }
            ],
            "downloads": "5k/m"
        }
    ]

However, if you want to take full advantage of the latest changes and have the most user-friendly CSV, then I'd also recommend specifying expandArrayObjects: true in your options object. That will tell the module that it should also parse array objects so that they're a bit more readable. The generated CSV when using this option will look like this for the above pasted JSON:

features.name
modules
testing

Thanks for reporting this and for your patience!

from json-2-csv.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.