zemirco / json2csv

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

Fields are missing in csv output #104

Closed adius closed 8 years ago

adius commented 8 years ago

I have several hundred objects and serialize them with json2csv. There are, however, some fields missing in the output. Must fields have a minimum number of occurrences, or why is that?

knownasilya commented 8 years ago

how many fields are there? and do all objects have those fields?

adius commented 8 years ago

Yeah, I just realized it's only the fields of the first object. Is there no flag to include all available fields in all objects and not just those of the first one?

knownasilya commented 8 years ago

It should work just fine as long as your JSON is an array. See this test: https://github.com/zemirco/json2csv/blob/master/test/index.js#L45

KaustubhYK commented 8 years ago

I am also facing same issue.

knownasilya commented 8 years ago

What does you data look like?

knownasilya commented 8 years ago

Ping

knownasilya commented 8 years ago

There is also a includeEmptyRows: true option (in CL --include-empty-rows or -a).

ghost commented 8 years ago

I'm experiencing the same problem. The output includes only the first objects in my multidimensional arrays. But i get the expected output using the flat module directly.

knownasilya commented 8 years ago

I need some example data to debug this issue.

rjcorwin commented 8 years ago

@knownasilya Here's an example of the behavior @adius explains as "it's only the fields of the first object". Note how the third object in the array has a different schema than the first two and the output only expresses schema of the first object and then behaves strangely for the output of the third object (I'm guessing it's something like undefined, undefined).

 /Users/rj λ json2csv --version
3.6.0
 /Users/rj λ echo '[{"a":1,"b":2}, {"a":3,"b":4},{"c":5}]' | json2csv
"a","b"
1,2
3,4
,
knownasilya commented 8 years ago

Looks like expected behavior to me, since you didn't set default values.

adius commented 8 years ago

If this is the default behavior, we have entirely different views on how this is supposed to work 😂🙈

adius commented 8 years ago

I seems like everybody else things it should work like this:

$ echo '[{"a":1,"b":2}, {"a":3,"b":4}, {"c":5}]' | json2csv
"a","b","c"
1,2,
3,4,
,,5
knownasilya commented 8 years ago

Ah, I see your point. I think this hasn't been addressed due to an assumption that all of the objects are of the same schema. I'd take a PR to address this if you want to contribute.

rjcorwin commented 8 years ago

@adius Your example is what I would have expected. Might be worth looking at how the jsonexport library handles "Complex Array", AKA an array of object with varying schemas.

knownasilya commented 8 years ago

So this issue only exists for the auto-fields feature, so it should be easy enough to fix. I'll look into it.

knownasilya commented 8 years ago

Can you give master a try before I publish this?

knownasilya commented 8 years ago

Published as 3.6.1

rjcorwin commented 8 years ago

It works!

Old...

$ echo '[{"a":1,"b":2}, {"a":3,"b":4},{"c":5}]' | json2csv
"a","b"
1,2
3,4
,

New!

$ echo '[{"a":1,"b":2}, {"a":3,"b":4}, {"c":5}]' | json2csv
"a","b","c"
1,2,
3,4,
,,5
clalcorn commented 5 years ago

Unless I am missing something, this is broken again:

json2csv -V
4.2.1
echo [{"a":1,"b":2}, {"a":3,"b":4},{"c":5}] | json2csv
"a","b"
1,2
3,4

,

Expected behavior should be: echo '[{"a":1,"b":2}, {"a":3,"b":4}, {"c":5}]' | json2csv "a","b","c" 1,2, 3,4, ,,5