nashwaan / xml-js

Converter utility between XML text and Javascript object / JSON text.
MIT License
1.27k stars 180 forks source link

json2xml no errors on conversion but empty file in result. #58

Open ntodorov opened 6 years ago

ntodorov commented 6 years ago

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

adam-knights commented 6 years ago

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?

nashwaan commented 6 years ago

@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}));
Mathijs003 commented 4 years ago

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

kr185204 commented 2 months ago

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