jsonexport icon indicating copy to clipboard operation
jsonexport copied to clipboard

CLI: Json Complex Object outputting all keys as headers

Open ChazUK opened this issue 4 years ago • 11 comments

I have a complex JSON object in a file that when running via the CLI outputs the keys as headers

jsonexport complex.json output.csv

complex.json

{
  "complex": {
    "object": 1,
    "that": 2
  },
  "has": 3,
  "nested": {
    "keys": true
  }
}

output.csv

complex.object,complex.that,has,nested.keys
1,2,3,true

but running it via the online demo page https://kaue.github.io/jsonexport/demo/?json={%22complex%22:{%22object%22:1,%22that%22:2},%22has%22:3,%22nested%22:{%22keys%22:true}}&options={}

it comes out as expected

complex.object,1
complex.that,2
has,3
nested.keys,true

ChazUK avatar Feb 04 '21 14:02 ChazUK

Acknowledged but not expecting immediate reaction without tighter connection to what is the cause.

I have an educated guess: The options in the demo, even when blank/default I believe, are still sending those blank/default values which perhaps are being seeing as altering arguments...

If you catch my drift above, our goal would be to console log the options in the demo and then exactly send those options in your NodeJs usage.

Based on your skill level, if you cloned our gh-pages branch and console logged the options at runtime you could get our answer. The file to edit would be gh-pages/demo/src/index.js@update:45

Good luck and may Feb 4th be with you

AckerApple avatar Feb 04 '21 15:02 AckerApple

Console logged options var from the gh-pages branch and it's an empty object. verticalOutput: true is still set as the default.

ChazUK avatar Feb 04 '21 16:02 ChazUK

edited the code and ran ./node_modules/jsonexport/bin/jsonexport.js a.json b.csv to output the CLI options

{
  headers: [],
  rename: [],
  headerPathString: '.',
  rowDelimiter: ',',
  textDelimiter: '"',
  arrayPathString: ';',
  undefinedString: '',
  endOfLine: '\n',
  mainPathItem: null,
  booleanTrueString: null,
  booleanFalseString: null,
  includeHeaders: true,
  fillGaps: false,
  verticalOutput: true,
  forceTextDelimiter: false }

which matches the default options of

{
	"headers": [],
	"rename": [],
	"headerPathString": ".",
	"rowDelimiter": ",",
	"textDelimiter": "\"",
	"arrayPathString": ";",
	"undefinedString": "",
	"endOfLine": "\n",
	"mainPathItem": null,
	"booleanTrueString": null,
	"booleanFalseString": null,
	"includeHeaders": true,
	"fillGaps": false,
	"verticalOutput": true,
	"forceTextDelimiter": false
}

Not sure if this matters but your bin/jsonexport.js which I'm guessing is what the command line uses is referring to lib/index.js rather than dist/index.js

ChazUK avatar Feb 04 '21 16:02 ChazUK

Hmmmmmm, nothing definitive coming from that... It was an educated guess at best but if no answers coming from that than we need more research and debugging

AckerApple avatar Feb 04 '21 17:02 AckerApple

The loaded file is being consumed as

[ { complex: { object: 1, that: 2 },
    has: 3,
    nested: { keys: true } } ]

looking into why, are you happy for me to create a PR once I find a fix?

ChazUK avatar Feb 04 '21 17:02 ChazUK

Looks like this is the culprit, https://github.com/kaue/jsonexport/blob/master/lib/core/stream.js#L45

I think

let data = self._wrapArray(parts.join("}"));
json = JSON.parse(data);

can just be

json = JSON.parse(chunk);

ChazUK avatar Feb 04 '21 17:02 ChazUK

By all means. Create the PR please and thank you. It may sit for a little due to schedules and such. I am just acting as front line communications, for this issue, so do not gain expectation of fast action.

AckerApple avatar Feb 04 '21 17:02 AckerApple

@AckerApple I have had a PR open for a few months now, has this been looked at or put into any kind of roadmap?

ChazUK avatar Sep 29 '21 08:09 ChazUK

This was news to me, as of just now. I will attempt to review soon.

AckerApple avatar Sep 29 '21 14:09 AckerApple

Oh hey I recall that now. I see you are getting help. I just contribute where I can here and there

AckerApple avatar Sep 29 '21 14:09 AckerApple

Yeah, we should be good here @AckerApple :) forgot to check the issues, still have a lot to catch up

kaue avatar Sep 29 '21 14:09 kaue