I try to convert an JSON Object into XML and back again in Javascript. The best so far I found is xml2js. But even this doesn't convert correct:
A simple json like this: {name:"name1", list:["entry1","entry2"]}
converts to xml like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
<name>name1</name>
<list>entry1</list>
<list>entry2</list>
</root>
going back to JSON I get this:
'{"root":{"name":["name1"],"list":["entry1","entry2"]}}'
The Problem is, that the name property is converted into a list []
My Code:
var xml2js = require('xml2js');
var testObject = {name:"name1", list:["entry1","entry2"]}
var builder = new xml2js.Builder();
var xmlOutput = builder.buildObject(testObject);
console.debug(xmlOutput);
var parseString = require('xml2js').parseString;
parseString(xmlOutput, function (err: any, result: any) {
var json = JSON.stringify(result)
console.dir(json);
});
Does anyone know whats wrong or suggest another converter to do this?