convert json to csv format

前端 未结 2 1243
花落未央
花落未央 2020-12-21 21:31

I try to use as input a file in json format. Here is a snippet of example data.

[
{
id: 1671349531,
name: \"A Wild Restaurant Expansion\",
blurb: \"We are lo         


        
2条回答
  •  不思量自难忘°
    2020-12-21 22:13

    I posted a couple of links in the comments as I was researching your answer, but now I am pretty convinced this is the way to do it.

    Install the node package, and then use the following with your own json string, (which I just lifted from the link above.):

    var converter = require('json-2-csv');
    
    var documents = [
        {
            Make: 'Nissan',
            Model: 'Murano',
            Year: '2013',
            Specifications: {
                Mileage: '7106',
                Trim: 'S AWD'
            }
        },
        {
            Make: 'BMW',
            Model: 'X5',
            Year: '2014',
            Specifications: {
                Mileage: '3287',
                Trim: 'M'
            }
        }
    ];
    
    var json2csvCallback = function (err, csv) {
        if (err) throw err;
        console.log(csv);
    };
    
    converter.json2csv(documents, json2csvCallback);
    

    This will return:

    Make,Model,Year,Specifications.Mileage,Specifications.Trim
    Nissan,Murano,2013,7106,S AWD
    BMW,X5,2014,3287,M
    

提交回复
热议问题