简体   繁体   English

使用node.js将嵌套的json转换为csv

[英]convert nested json to csv using node.js

I want to convert a nested json to csv file using node.js. 我想使用node.js将嵌套的json转换为csv文件。 my JSON structure: 我的JSON结构:

[
{
    "Make": "Nissan",
    "Model": "Murano",
    "Year": "2013",
    "Specifications": {
        "Mileage": "7106",
        "Trim": "SAWD"
    },
    "Items": [
        {
            "flavor": {
                "name": "Cherry",
                "id": 1
            },
            "packSize": {
                "name": "200ML",
                "id": 1
            }
        },
        {
            "flavor": {
                "name": "Vanilla",
                "id": 2
            },
            "packSize": {
                "name": "300ML",
                "id": 2
            }
        }
    ]
},
{
    "Make": "BMW",
    "Model": "X5",
    "Year": "2014",
    "Specifications": {
        "Mileage": "3287",
        "Trim": "M"
    },
    "Items": [
        {
            "flavor": {
                "name": "Cherry",
                "id": 1
            },
            "packSize": {
                "name": "200ML",
                "id": 1
            }
        },
        {
            "flavor": {
                "name": "Vanilla",
                "id": 2
            },
            "packSize": {
                "name": "300ML",
                "id": 2
            }
        }
    ]
}
]

I have used 'json-2-csv' module but it only converts the simple structure not the nested structure. 我使用'json-2-csv'模块,但它只转换简单结构而不是嵌套结构。 only the 'make','model','year' and 'specification' is converted,'items' are not converted How to do this??? 只有'make','model','year'和'specification'被转换,'items'没有转换怎么做?

You can use the module jsonexport its pretty easy, check this sample: 您可以使用jsonexport模块非常简单,请查看此示例:

Here is the output using the json you provided and jsonexport: 这是使用您提供的json和jsonexport的输出: 在此输入图像描述

Sample: 样品:

var jsonexport = require('jsonexport');

var contacts = [{
   name: 'Bob',
   lastname: 'Smith',
   family: {
       name: 'Peter',
       type: 'Father'
   }
},{
   name: 'James',
   lastname: 'David',
   family:{
       name: 'Julie',
       type: 'Mother'
   }
},{
   name: 'Robert',
   lastname: 'Miller',
   family: null,
   location: [1231,3214,4214]
},{
   name: 'David',
   lastname: 'Martin',
   nickname: 'dmartin'
}];

jsonexport(contacts,function(err, csv){
    if(err) return console.log(err);
    console.log(csv);
});

The ouput: 输出:

lastname;name;family.type;family.name;nickname;location
Smith;Bob;Father;Peter;;
David;James;Mother;Julie;;
Miller;Robert;;;;1231,3214,4214
Martin;David;;;dmartin;

Source: https://www.npmjs.com/package/jsonexport 资料来源: https//www.npmjs.com/package/jsonexport

Do you always have the same numbers of columns ? 你总是有相同数量的列吗? ie : Do you have a fixed (or max number) of items ? 即:你有固定(或最大数量)的物品吗?

Make;Model;Year;Mileage;Trim;Item_1_flavor_name;Item_1_packSize_name;Item_2_flavor_name;Item_2_packSize_name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM