json2csv icon indicating copy to clipboard operation
json2csv copied to clipboard

Fields are missing in csv output

Open adius opened this issue 10 years ago • 20 comments

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?

adius avatar Feb 23 '16 09:02 adius

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

knownasilya avatar Feb 23 '16 13:02 knownasilya

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?

adius avatar Feb 23 '16 15:02 adius

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

knownasilya avatar Feb 23 '16 15:02 knownasilya

I am also facing same issue.

KaustubhYK avatar Mar 18 '16 09:03 KaustubhYK

What does you data look like?

knownasilya avatar Mar 18 '16 11:03 knownasilya

Ping

knownasilya avatar May 23 '16 14:05 knownasilya

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

knownasilya avatar Jun 21 '16 13:06 knownasilya

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.

ghost avatar Jun 29 '16 21:06 ghost

I need some example data to debug this issue.

knownasilya avatar Jun 30 '16 01:06 knownasilya

@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
,

rjcorwin avatar Jul 10 '16 16:07 rjcorwin

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

knownasilya avatar Jul 10 '16 18:07 knownasilya

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

adius avatar Jul 10 '16 18:07 adius

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

adius avatar Jul 10 '16 18:07 adius

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.

knownasilya avatar Jul 10 '16 19:07 knownasilya

@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.

rjcorwin avatar Jul 11 '16 00:07 rjcorwin

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

knownasilya avatar Jul 11 '16 12:07 knownasilya

Can you give master a try before I publish this?

knownasilya avatar Jul 12 '16 00:07 knownasilya

Published as 3.6.1

knownasilya avatar Jul 12 '16 13:07 knownasilya

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

rjcorwin avatar Jul 15 '16 01:07 rjcorwin

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

clalcorn avatar Nov 15 '18 20:11 clalcorn