0

I'm a total JS noob. I have read a JSON and filtered out specific items from it and saved it to a variable named mapped. How do I export this JSON to a properly formatted CSV file?

let json = require('./users.json')
let filtered = json.Users.filter((a)=>{
    return new Date(a.UserCreateDate) > new Date('2020-05-11T00:00:00.000000+05:30')
})
let mapped=filtered.map((a)=>{
    let email
    a.Attributes.forEach(element => {
        if(element.Name=='email'){
            email = element.Value
        }
    });
    return {
        name: a.Username,
        email: email,
        UserCreateDate: a.UserCreateDate,
        UserStatus: a.UserStatus
    }
})
console.log(JSON.stringify(mapped, null, 4), mapped.length)

Although there are quite a few answers to this topic, I haven't been able to successfully implement any of those.

Devarshi Goswami
  • 439
  • 1
  • 11
  • It's not clear what kind of result you want. If it turns to CSV file, you want the keys to be the header or just list keys and values together in a line? – Carr May 16 '20 at 08:45
  • 1
    Does this answer your question? [How can I convert JSON to CSV?](https://stackoverflow.com/questions/1871524/how-can-i-convert-json-to-csv) – Momin May 16 '20 at 08:46
  • Does this answer your question? [How to convert JSON to CSV format and store in a variable](https://stackoverflow.com/questions/8847766/how-to-convert-json-to-csv-format-and-store-in-a-variable) – pritam May 16 '20 at 08:46
  • 1
    @Momin, your reference is for python ... – Carr May 16 '20 at 09:29
  • @pritam I did try the samples in the code you mentioned but I believe its use case was a tad different. – Devarshi Goswami May 16 '20 at 09:31

1 Answers1

1

I assume you wanna use the keys as the header in CSV file. What you need is to open a write stream, the format of the CSV file is pure strings with commas separating the values.

// ...
let mapped=filtered.map((a)=>{
  //...
})

// ...

const fs = require('fs');
let writeStream = fs.createWriteStream('./output.csv');
writeStream.on('open', () => {
  // get the header
  const header = Object.keys(mapped[0]).toString().concat('\n');

  writeStream.write(header);

  mapped.forEach((obj) => {
    const values = Object.values(obj).toString().concat('\n');
    writeStream.write(values);
  });
  writeStream.end();
});

Carr
  • 2,331
  • 1
  • 16
  • 25
  • i get ReferenceError: object1 is not defined – Devarshi Goswami May 16 '20 at 09:08
  • oh, that is typo – Carr May 16 '20 at 09:09
  • Also, if I use map func instad of filter I get another error at line ` a.Attributes.forEach(element => {` – Devarshi Goswami May 16 '20 at 09:10
  • What kind of problem? – Carr May 16 '20 at 09:11
  • TypeError: Cannot read property 'forEach' of undefined.. I do get a CSV with your suggested code. Thanks! but there needs to be a delimiter after one row of data cuz I'm getting everything in the same row.How do I do that? – Devarshi Goswami May 16 '20 at 09:14
  • and this is how its showing, with the headers inside the cells as well ..```name email UserCreateDate UserStatus10mintest07 hru58109@zzrgg.com 2020-05-11T19:45:28.642000+05:30 CONFIRMED10mintest08 flmhiknaajfmbnenxf@awdrt.com ``` – Devarshi Goswami May 16 '20 at 09:15
  • @DevarshiGoswami, sorry about the filter part, it's my bad I overlook it, you just keep your code in that part, sorry! I just show the same code as you already did. – Carr May 16 '20 at 09:17
  • @DevarshiGoswami, I updated the answer, just append one more `\n` for a newline. – Carr May 16 '20 at 09:23