0

I am working on node Javascript application. I have an array, I want to transform this array into a CSV data of specific attributes.

var data = 
[ 
  {
    "Name":"nom1",
    "Division":"first",
    "Cities":['city1','city2']
  },
  {
    "Name":"nom2",
    "Division":"first",
    "Cities":['city4','city5']
  }
]

I want this array to transformed to ,

 Name,Cities

 nom1,city1

 nom1,city2 

 nom2,city3 

 nom2,city4 

I tried using npm package json2csv it repeats the header on each line

script.js

const { Parser, transforms: { unwind } } = require('json2csv');
const fs = require('fs');
const path = require('path');

let outputFile = path.resolve(__dirname + path.sep + './output.csv');

const myData = [ 
    {
      "Name":"nom1",
      "Division":"first",
      "Cities":['city1','city2']
    },
    {
      "Name":"nom2",
      "Division":"first",
      "Cities":['city4','city5']
    }
  ]

const fields = ['Name','Cities'];
const transforms = [unwind({ paths: ['Cities'] })];

for ( i=0; i < 2; i++ ) {

    const json2csvParser = new Parser({ fields, transforms });
    const csv = json2csvParser.parse(myCars[i]);
    fs.appendFileSync(outputFile,csv);
}

But this creates data of the form instead of what I need

"Name","Cities"
"nom1","city1"
"nom1","city2""Name","Cities"
"nom2","city4"
"nom2","city5"

My requirement is to transform the data , I am open to use any new technology or other npm package. Can anyone suggest me what to do?

5
  • 1
    what is this myCars[i] ?. I think since you are looping the headers keeps adding so you need to avoid that part Commented May 16, 2020 at 19:58
  • Thanks @DILEEPTHOMAS that was good catch , I removed the loop it worked Commented May 16, 2020 at 20:06
  • I have added it as the answer since there is no codeblock to be added just the statement is fine, feel free to edit. If it worked for you kindly accept and vote it so it will be helpful for others. Commented May 16, 2020 at 20:11
  • Also add the snippet that you have removed on the answer so it will be helpful :) happy coding Commented May 16, 2020 at 20:12
  • There is no JSON involved here; myData is an array of objects. Commented May 16, 2020 at 20:17

2 Answers 2

1

The issue was since you are looping through it, it will create the headers multiple times.

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

Comments

1

Removing for loop and passing the whole data array solved this problem

const { Parser, transforms: { unwind } } = require('json2csv');
const fs = require('fs');
const path = require('path');

let outputFile = path.resolve(__dirname + path.sep + './output.csv');

const myData = [ 
    {
      "Name":"nom1",
      "Division":"first",
      "Cities":['city1','city2']
    },
    {
      "Name":"nom2",
      "Division":"first",
      "Cities":['city4','city5']
    }
  ]

const fields = ['Name','Cities'];
const transforms = [unwind({ paths: ['Cities'] })];


    const json2csvParser = new Parser({ fields, transforms });
    const csv = json2csvParser.parse(myCars);
    fs.appendFileSync(outputFile,csv);

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.