Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fields are missing in csv output #104

Closed
adius opened this issue Feb 23, 2016 · 20 comments
Closed

Fields are missing in csv output #104

adius opened this issue Feb 23, 2016 · 20 comments
Assignees
Labels

Comments

@adius
Copy link

adius commented Feb 23, 2016

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
Copy link
Collaborator

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

@adius
Copy link
Author

adius commented Feb 23, 2016

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
Copy link
Collaborator

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
Copy link

I am also facing same issue.

@knownasilya
Copy link
Collaborator

What does you data look like?

@knownasilya
Copy link
Collaborator

Ping

@knownasilya
Copy link
Collaborator

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

@ghost
Copy link

ghost commented Jun 29, 2016

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
Copy link
Collaborator

I need some example data to debug this issue.

@rjcorwin
Copy link

rjcorwin commented Jul 10, 2016

@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
Copy link
Collaborator

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

@adius
Copy link
Author

adius commented Jul 10, 2016

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

@adius
Copy link
Author

adius commented Jul 10, 2016

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
Copy link
Collaborator

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
Copy link

@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
Copy link
Collaborator

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

@knownasilya
Copy link
Collaborator

Can you give master a try before I publish this?

knownasilya pushed a commit that referenced this issue Jul 12, 2016
* Fix auto-fields returning all avilable fields, even if not available on the first object, see #104
@knownasilya
Copy link
Collaborator

Published as 3.6.1

@rjcorwin
Copy link

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants