zemirco / json2csv

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

Opts object being modified by parsing method #317

Closed gilles-crealp closed 6 years ago

gilles-crealp commented 6 years ago
let Json2csvParser = require('json2csv').Parser
let json2csvParserOptions = {
  header: false
}

let json2csv = new Json2csvParser(json2csvParserOptions)
console.log(json2csv.parse([
  {
    'a': 1,
    'b': 2
  },
  {
    'a': 3,
    'b': 4
  }
]))
// Output:
// 1,2
// 3,4

console.log()

json2csv = new Json2csvParser(json2csvParserOptions)
console.log(json2csv.parse([
  {
    'a': 1,
    'x': 2
  },
  {
    'a': 3,
    'x': 4
  }
]))
// Output:
// 1,
// 3,
// Should be:
// 1,2
// 3,4

Versions:

gilles-crealp commented 6 years ago

Shouldn't be the same if I don't define fields and parse multiple different data? So I can define the options (except fields) only once.

let Json2csvParser = require('json2csv').Parser
let json2csv = new Json2csvParser({
  header: false
})

console.log(json2csv.parse([
  {
    "a": 1,
    "b": 2
  },
  {
    "a": 3,
    "b": 4
  }
]))
// Output:
// 1,2
// 3,4

console.log(json2csv.parse([
  {
    "a": 1,
    "x": 2
  },
  {
    "a": 3,
    "x": 4
  }
]))
// Output:
// 1,
// 3,
// Should be?
// 1,2
// 3,4
juanjoDiaz commented 6 years ago

It's a bug indeed!! I'll fix it asap. Thanks for reporting with such a clear code sample :)

gilles-crealp commented 6 years ago

Not so good: I didn't figure out that the problem was when you use header: false only. ;)

I've change the sample code.

gilles-crealp commented 6 years ago

And you need to have the same object for the options!

Work:

let Json2csvParser = require('json2csv').Parser

let json2csv = new Json2csvParser({
  header: false
})
console.log(json2csv.parse([
  {
    'a': 1,
    'b': 2
  },
  {
    'a': 3,
    'b': 4
  }
]))
// Output:
// 1,2
// 3,4

console.log()

json2csv = new Json2csvParser({
  header: false
})
console.log(json2csv.parse([
  {
    'a': 1,
    'x': 2
  },
  {
    'a': 3,
    'x': 4
  }
]))
// Output:
// 1,2
// 3,4

Do not work:

let Json2csvParser = require('json2csv').Parser
let json2csvParserOptions = {
  header: false
}

let json2csv = new Json2csvParser(json2csvParserOptions)
console.log(json2csv.parse([
  {
    'a': 1,
    'b': 2
  },
  {
    'a': 3,
    'b': 4
  }
]))
// Output:
// 1,2
// 3,4

console.log()

json2csv = new Json2csvParser(json2csvParserOptions)
console.log(json2csv.parse([
  {
    'a': 1,
    'x': 2
  },
  {
    'a': 3,
    'x': 4
  }
]))
// Output:
// 1,
// 3,
juanjoDiaz commented 6 years ago

Yeah. The problem is that running any parsing modified the options object. Can't believe that no one noticed until now... (including myself).

Once again, thanks for reporting. Will be fixed whenever the PR get's merged.

knownasilya commented 6 years ago

Fix published in v4.2.1

gilles-crealp commented 6 years ago

Thx for the reactivity!

knownasilya commented 6 years ago

Thanks for the detailed issue 💃