xml-js
xml-js copied to clipboard
json2xml no errors on conversion but empty file in result.
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
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?
@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}));
This really should be in the documentation, I was searching for an hour