zemirco / json2csv

Convert json to csv with column titles
http://zemirco.github.io/json2csv
MIT License
2.71k stars 366 forks source link

Field Labels Aren't Being Used #537

Closed selected-pixel-jameson closed 3 years ago

selected-pixel-jameson commented 3 years ago

For some reason my field labels are not being rendered. It just continues to use the object values in the data set that I'm sending through. Sorry if this is something small that I'm missing. I tried to search for past issues, but didn't find anything that seemed relevant.

5.0.6 NodeJS 10.x

Fields

const fields = [
  {
    label: 'Name',
    value: 'name'
  },
  {
    label: 'Email',
    value: 'email'
  },
  {
    label: 'Practitioner',
    value: 'practitioner'
  },
  {
    label: 'Event',
    value: 'referrer'
  },
  {
    label: 'Topic',
    value: 'topic'
  },
  {
    label: 'Code',
    value: 'code'
  },
  {
    label: 'Offer ID',
    value: 'htoOfferId'
  }
]
let data = [{
  name: "Alannak",
  email: "xxxxxxx",
  practitioner: 0,
  referrer: "mentalwellness.xxxxxx.com",
  topic: "Mental Health",
  code: "MNWL20",
  htoOfferId: null,
}, ..... ]
let json2csv = new Parser({ fields })
let csv = json2csv.parse(data)

This is what is returned. Please note this is just sample data.

"name","email","practitioner","referrer","topic","code","htoOfferId" "Gentjan","xxxxxxxx",0,"diydetoxsummit.byhealthmeans.com","Detox","DIY21", "Laura","xxxxxxx",0,"adrenalsolutions.byhealthmeans.com",,"ADRL21", "Gnanagirija","xxxxxxxxxx",0,"adrenalsolutions.byhealthmeans.com",,"ADRL21",

nahumdam commented 3 years ago

same problem here!.. any solutions yet?

knownasilya commented 3 years ago

Can one of you try https://github.com/zemirco/json2csv/blob/master/CHANGELOG.md#600-alpha0-2021-04-14

nahumdam commented 3 years ago

installed 6.0.0-alpha.. problem persists

knownasilya commented 3 years ago

So the fields for the parser only take an array of strings at this time. Looking to see if that's a mistake or not.

knownasilya commented 3 years ago

Honestly it should work, the tests show it working: https://github.com/zemirco/json2csv/blob/master/test/JSON2CSVParser.js#L294

Do either of you see anything different you are doing? @nahumdam esp you since this is v6

juanjoDiaz commented 3 years ago

I just ran you example on json2csv 5.0.6

var { Parser } = require("json2csv")

const fields = [
  {
    label: 'Name',
    value: 'name'
  },
  {
    label: 'Email',
    value: 'email'
  },
  {
    label: 'Practitioner',
    value: 'practitioner'
  },
  {
    label: 'Event',
    value: 'referrer'
  },
  {
    label: 'Topic',
    value: 'topic'
  },
  {
    label: 'Code',
    value: 'code'
  },
  {
    label: 'Offer ID',
    value: 'htoOfferId'
  }
]
let data = [{
  name: "Alannak",
  email: "xxxxxxx",
  practitioner: 0,
  referrer: "mentalwellness.xxxxxx.com",
  topic: "Mental Health",
  code: "MNWL20",
  htoOfferId: null,
}]
let json2csv = new Parser({ fields })
let csv = json2csv.parse(data)

console.log(csv)

the result was exactly as expected

"Name","Email","Practitioner","Event","Topic","Code","Offer ID"
"Alannak","xxxxxxx",0,"mentalwellness.xxxxxx.com","Mental Health","MNWL20",

So not sure what you are doing differently...

nahumdam commented 3 years ago

I tried your code and it worked, thanks.

For some reason, I just renamed my variable to "fields" and suddenly it worked. If I call it any other way, labels do not work... which makes no sense, but I'll research more on it later

knownasilya commented 3 years ago
let json2csv = new Parser({ fields: someOtherName })

Should work

nahumdam commented 3 years ago

you are right, it does work! thanks

I think this is our answer @selected-pixel-jameson add "fields:" before the name of your variable on the new Parser declaration

selected-pixel-jameson commented 3 years ago

Thanks! Might want to update the docs.