xml-js icon indicating copy to clipboard operation
xml-js copied to clipboard

json2xml no errors on conversion but empty file in result.

Open ntodorov opened this issue 6 years ago • 4 comments

Hi,

Can you guide me how can I find why the conversion returns an empty object? There is no error that can give me a hint what is the problem...

Please see this code: https://runkit.com/niktv/5ad4fad6772a1e00129ebe64

Thanks, Nik

ntodorov avatar Apr 16 '18 19:04 ntodorov

Looking at line 304 onward of https://github.com/nashwaan/xml-js/blob/master/lib/js2xml.js - it looks like if you supply no options at all then an empty string will be returned.

js2xml is called by json2xml FYI.

It feels like an improvement could be made here to either make the README clearer, or throw an error if none of these options are hit?

adam-knights avatar Apr 20 '18 12:04 adam-knights

@ntodorov , Sorry for late reply. By default, this library expects json input to be in non-compact format. See the example in Synopsis section to know the difference. Read here to know more why this library supports compact format and non-compact format.

Since your input is in compact format, then you need to tell the library about this explicitly, otherwise it assumes your input is in non-compact format. To fix your example, just past {compact: true} option to the converter:

console.log(xmlJs.json2xml(my_json, {compact: true, spaces: 4}));

nashwaan avatar May 17 '18 06:05 nashwaan

This really should be in the documentation, I was searching for an hour

Mathijs003 avatar Dec 16 '19 10:12 Mathijs003