5

I want to convert a nested json to csv file using node.js. my JSON structure:

[
{
    "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. only the 'make','model','year' and 'specification' is converted,'items' are not converted How to do this???

2 Answers 2

7

You can use the module jsonexport its pretty easy, check this sample:

Here is the output using the json you provided and jsonexport: enter image description here

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

Sign up to request clarification or add additional context in comments.

7 Comments

for the json value, i've mentioned earlier,it's not working properly.Please try with that json array and mention a proper solution.
I just tested with jsonexport and i got this output, is that what you was expecting ?Items.packSize.id,Items.packSize.name,Items.flavor.id,Items.flavor.name,Specifications.Trim,Specifications.Mileage,Year,Model,Make 1,200ML,1,Cherry,SAWD,7106,2013,Murano,Nissan 2,300ML,2,Vanilla,,,,, 1,200ML,1,Cherry,M,3287,2014,X5,BMW 2,300ML,2,Vanilla,,,,,
No,Where are the values of make,model and year.The output has some blank value.
@Subham I included a picture in my answer so you will be able to understand what the CSV means and where are the model and year fields.
It looks like the library has a fillGaps option that will prevent missing data. See npmjs.com/package/jsonexport documentation.
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.