Closed adius closed 8 years ago
how many fields are there? and do all objects have those fields?
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?
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
I am also facing same issue.
What does you data look like?
Ping
There is also a includeEmptyRows: true
option (in CL --include-empty-rows
or -a
).
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.
I need some example data to debug this issue.
@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
,
Looks like expected behavior to me, since you didn't set default values.
If this is the default behavior, we have entirely different views on how this is supposed to work 😂🙈
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
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.
@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.
So this issue only exists for the auto-fields feature, so it should be easy enough to fix. I'll look into it.
Can you give master a try before I publish this?
Published as 3.6.1
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
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
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?