JSON to XML Using Javascript
Solution 1
replace your OBJtoXML
function with
function OBJtoXML(obj) {
var xml = '';
for (var prop in obj) {
xml += obj[prop] instanceof Array ? '' : "<" + prop + ">";
if (obj[prop] instanceof Array) {
for (var array in obj[prop]) {
xml += "<" + prop + ">";
xml += OBJtoXML(new Object(obj[prop][array]));
xml += "</" + prop + ">";
}
} else if (typeof obj[prop] == "object") {
xml += OBJtoXML(new Object(obj[prop]));
} else {
xml += obj[prop];
}
xml += obj[prop] instanceof Array ? '' : "</" + prop + ">";
}
var xml = xml.replace(/<\/?[0-9]{1,}>/g, '');
return xml
}
Solution 2
There are a few problems here, for starters, here the JSON string variable either needs to have it's quotes escaped. Or be wrapped in single quotes. For example:
var InputJSON = '{"body":{"entry": [{ "fullURL" : "abcd","Resource": "1234"},{ "fullURL" : "efgh","Resource": "5678"}]}}';
Next, there is no need to use eval
here, when using JSON in JavaScript you should use JSON.parse
// First parse the JSON
var InputJSON = JSON.parse(InputJSON);
// Now execute the 'OBJtoXML' function
var output = OBJtoXML(InputJSON);
Now we come to the meat of this question, why is entry
only occuring once?
The problem that you're having is that xml += "<" + prop + ">";
and xml += "</" + prop + ">";
are only happening once per property.
A possible solution would look like this:
function OBJtoXML(obj) {
var xml = '';
for (var prop in obj) {
xml += "<" + prop + ">";
if(Array.isArray(obj[prop])) {
for (var array of obj[prop]) {
// A real botch fix here
xml += "</" + prop + ">";
xml += "<" + prop + ">";
xml += OBJtoXML(new Object(array));
}
} else if (typeof obj[prop] == "object") {
xml += OBJtoXML(new Object(obj[prop]));
} else {
xml += obj[prop];
}
xml += "</" + prop + ">";
}
var xml = xml.replace(/<\/?[0-9]{1,}>/g,'');
return xml
}
Solution 3
Using xml-js lib
import { json2xml } from "xml-js";
const input = {
contact: {
name: `John & cia "example"`
}
};
const xml = json2xml(input, {
compact: true
});
// <contact><name>John & cia \"example\"</name></contact>
https://codesandbox.io/s/xml-json-forked-zgit4?file=/src/index.js:97-103
:)
Solution 4
Xml-to-json library has method jsonToXml(json)
.
var inputJSON = '{"body":{"entry": [{ "fullURL" : "abcd","Resource": "1234"},{ "fullURL" : "efgh","Resource": "5678"}]}}';
var xml = jsonToXml(inputJSON);
// <?xml version="1.0" encoding="UTF-8"?>
// <body>
// <entry>
// <fullURL>abcd</fullURL>
// <Resource>1234</Resource>
// </entry>
// <entry>
// <fullURL>efgh</fullURL>
// <Resource>5678</Resource>
// </entry>
// </body>
Solution 5
var inputJSON = '{"body":{"entry": [{ "fullURL" : "abcd","Resource": "1234"},{ "fullURL" : "efgh","Resource": "5678"}]}}';
var parsedInput = JSON.parse(inputJSON);
function OBJtoXML(obj) {
var xml = '';
for (var prop in obj) {
if (obj[prop] instanceof Array) {
for (var array in obj[prop]) {
xml += '<' + prop + '>';
xml += OBJtoXML(new Object(obj[prop][array]));
xml += '</' + prop + '>';
}
} else {
xml += '<' + prop + '>';
typeof obj[prop] == 'object' ? xml += OBJtoXML(new Object(obj[prop])) : xml += obj[prop];
xml += '</' + prop + '>';
}
}
var xml = xml.replace(/<\/?[0-9]{1,}>/g, '');
return xml;
}
nkn
Updated on July 17, 2022Comments
-
nkn almost 2 years
I am trying to convert the JSON to XML but not getting exact output.In My JSON having array object it not converting that to XML array.Mainly array object is not converting into XML as expected
var InputJSON = "{"body":{"entry": [{ "fullURL" : "abcd","Resource": "1234"},{ "fullURL" : "efgh","Resource": "5678"}]}}"; var output = eval("OBJtoXML("+InputJSON+");") function OBJtoXML(obj) { var xml = ''; for (var prop in obj) { xml += "<" + prop + ">"; if(obj[prop] instanceof Array) { for (var array in obj[prop]) { xml += OBJtoXML(new Object(obj[prop][array])); } } else if (typeof obj[prop] == "object") { xml += OBJtoXML(new Object(obj[prop])); } else { xml += obj[prop]; } xml += "</" + prop + ">"; } var xml = xml.replace(/<\/?[0-9]{1,}>/g,''); return xml }
Actual Output:
<body> <entry> <fullURL>abcd</fullURL> <Resource>1234</Resource> <fullURL>efgh</fullURL> <Resource>5678</Resource> </entry> </body>
Expected Output:
<body> <entry> <fullURL>abcd</fullURL> <Resource>1234</Resource> </entry> <entry> <fullURL>efgh</fullURL> <Resource>5678</Resource> </entry> </body>
Please guide me if i am missing anything from the code to get my expected result
-
nkn over 6 yearsGreat Usman it works as expected...Thanks for your prompt response.. :)
-
nkn over 6 yearsThanks for your solution @Anderi Ros.Here i am trying without any inbuild libraries to parse the JSON.So i have used "eval".
-
Tom McDonald almost 4 yearsthis doesn't work if the attribute or property contains a slash. fore example a json property of "team/stats" wont work as it will be converted to <team/stats> which is invalid xml.
-
Valentyn Kolesnikov about 3 yearsutilities-online.info/xmltojson uses this library.