jsonexport icon indicating copy to clipboard operation
jsonexport copied to clipboard

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

Open MelissaSnell opened this issue 5 years ago • 12 comments

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

MelissaSnell avatar Oct 21 '20 12:10 MelissaSnell

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

AckerApple avatar Oct 21 '20 12:10 AckerApple

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

MelissaSnell avatar Oct 21 '20 14:10 MelissaSnell

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.

AckerApple avatar Oct 21 '20 14:10 AckerApple

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 :-)

MelissaSnell avatar Oct 21 '20 14:10 MelissaSnell

@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

AckerApple avatar Oct 22 '20 01:10 AckerApple

Great! I will take a look. Thank you.

MelissaSnell avatar Oct 22 '20 09:10 MelissaSnell

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

bestekov avatar Apr 07 '22 01:04 bestekov

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

AckerApple avatar Apr 07 '22 13:04 AckerApple

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 avatar Apr 30 '22 14:04 bestekov

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

bestekov avatar Jul 17 '22 16:07 bestekov

@bestekov try using the headers option.

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

kaue avatar Jul 17 '22 19:07 kaue

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

kaue avatar Jul 17 '22 20:07 kaue