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

Question. What is expected behaviour when exporting objects where some do not have all fields in header #87

Open
MelissaSnell opened this issue Oct 21, 2020 · 12 comments

Comments

@MelissaSnell
Copy link

Hi there

Really like how easy your library is to use :-)

I have a JSON array of objects , where the objects may or may not have all of the fields in the header.

What I am seeing in the output is that missing fields in the "middle" of the header correctly have ",,," but I don't think that trailing commas are being added for fields missing at the end of a row.

So I have some rows that have all 20 cols, but others that only have commas for 15 due to fields towards the end of the header.

Is that the expected behaviour?

Many thanks in advance

@AckerApple
Copy link
Collaborator

AckerApple commented Oct 21, 2020

Build an example of your issue here and then paste us the link:
https://kaue.github.io/jsonexport/demo/

Then include annotated screen capture highlighting issue on that page.

We need better examples and illustrations from you please and thank you

@MelissaSnell
Copy link
Author

I think that the issue happens when you get child objects that may not have the same fields for each record. I have updated the demo JSON with an "interests" object.

I looked at the header and then made sure that someone with a nickname had the Interests object.

As a result, Bob and James are not correct because they are missing trailing commas to complete the line.
Only David has the correct number of commas (and this is because he has a nick name and this is the last header entry).
I think that there should be the same number of comma delimiters per row as there are header entries?

Here is the JSON for the demo:

[
{
"name":"Bob",
"lastname":"Smith",
"family":{
"name":"Peter",
"type":"Father"
},
"interests":{
"music":"jazz"
}
},
{
"name":"James",
"lastname":"David",
"family":{
"name":"Julie",
"type":"Mother"
},
"interests":{
"books":"pulp fiction"
}
},
{
"name":"Robert",
"lastname":"Miller",
"family":null,
"location":[
1231,
3214,
4214
]
},
{
"name":"David",
"lastname":"Martin",
"nickname":"dmartin",
"interests":{
"books":"pulp fiction"
}
}
]

and this is the CSV you get

name,lastname,family.name,family.type,interests.music,interests.books,family,location,nickname
Bob,Smith,Peter,Father,jazz -MISSING TRAILING COMMAS
James,David,Julie,Mother,,pulp fiction - MISSING TRAILING COMMAS
Robert,Miller,,,,,,1231;3214;4214 -MISSING TRAILING COMMAS
David,Martin,,,,pulp fiction,,,dmartin - OK

@AckerApple
Copy link
Collaborator

Simplified direct link to reproduce issue here

Ok, I can see what you are calling out in that we need extra commas at the end.

Not sure who will get to this and we invite you to try and make a PR for it as well. I do not have time currently to address in any immediate fashion.

@MelissaSnell
Copy link
Author

Sure - not a problem. If we can contribute then I will have a go if I can find some time! Same for all of us :-)

@AckerApple
Copy link
Collaborator

@MelissaSnell I just noticed pull request #81 might just be your fix. It has conflicts right now but if worked out I believe it aims to address your issue

@MelissaSnell
Copy link
Author

Great! I will take a look. Thank you.

@bestekov
Copy link

bestekov commented Apr 7, 2022

Any hope of that feature in #81 being merged? The need to deal with sparse objects is very real.

@AckerApple
Copy link
Collaborator

@bestekov have you checked out and tried #81? I tried briefly reviewing and I'm too out of context with what's going on to verify. It may help to have you provide insight here and any supporting evidence of it being a needed solution.

I read through past comments and saw we were going to work towards confirmations ourselves but life.

@bestekov
Copy link

Will see if I can get some time to try to run tests. But the basic gist is there should be a unit test of an object like:

[
  { foo: 'a1', bar: 'b1' },
  { foo: 'a2', bar: 'b2', qux: 'd2' },
  { foo: 'a3', bar: 'b3', baz: 'c3' }
]

And that should result in:

foo,bar,qux,baz
a1,b1,,
a2,b2,d2,
a3,b3,,c3

Where today without a fix you instead get:

foo, bar, qux, baz
a1,b1               // the lack of trailing commas breaks things
a2,b2,d2
a3,b3,,c3

@bestekov
Copy link

@AckerApple , I tried #81 , but it did not appear to fix this issue. Any hope of a fix to trailing commas?

Or, alternatively, is there any way to specify that it should output columns in a particular order? Then maybe I could put a column I know will always have a value at the end of the header list to ensure that extra commas are inserted internally to the row (since that appears to work okay).

@kaue
Copy link
Owner

kaue commented Jul 17, 2022

@bestekov try using the headers option.

headers - Array Used to set a custom header order, defaults to [] example ['lastname', 'name']

@kaue
Copy link
Owner

kaue commented Jul 17, 2022

I will try catching up with this issue and with PR reviews when i get some free time.

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

No branches or pull requests

4 participants