JSON file to CSV in Node.js

0

I would like to know if anyone has an idea of how to convert a JSON into CSV in node.js

For example, this JSON:

var myObj = {
"Processes": [{
    "id": "p1",
    "burstTime": "3",
    "arribo": "0"
}, {
    "id": "p2",
    "burstTime": "4",
    "arribo": "1"
}, {
    "id": "p3",
    "burstTime": "5",
    "arribo": "2"
    }]
}

I have read that node.js has several libraries but I still can not use them properly.

HELP !!

    
asked by java005 08.11.2018 в 04:07
source

1 answer

0

See this package: link

For your punctual example you can use it in the following way

const Json2csvParser = require('json2csv').Parser;
const fields = ['id', 'burstTime', 'arribo'];
const myCars = [{
    "id": "p1",
    "burstTime": "3",
    "arribo": "0"
}, {
    "id": "p2",
    "burstTime": "4",
    "arribo": "1"
}, {
    "id": "p3",
    "burstTime": "5",
    "arribo": "2"
}];

const json2csvParser = new Json2csvParser({ fields });
const csv = json2csvParser.parse(myCars);

console.log(csv);

Then you can write csv to a file or whatever you need

    
answered by 08.11.2018 в 14:46