We can use the xml-js
library to easily convert a JSON string to an XML string in JavaScript.
import { json2xml } from 'xml-js';
const jsonObj = {
name: 'Garage',
cars: [
{ color: 'red', maxSpeed: 120, age: 2 },
{ color: 'blue', maxSpeed: 100, age: 3 },
{ color: 'green', maxSpeed: 130, age: 2 },
],
};
const json = JSON.stringify(jsonObj);
const xml = json2xml(json, { compact: true, spaces: 4 });
console.log(xml);
This code will have the following output:
<name>Garage</name>
<cars>
<color>red</color>
<maxSpeed>120</maxSpeed>
<age>2</age>
</cars>
<cars>
<color>blue</color>
<maxSpeed>100</maxSpeed>
<age>3</age>
</cars>
<cars>
<color>green</color>
<maxSpeed>130</maxSpeed>
<age>2</age>
</cars>
Install xml-js
Before using xml-js
, we’ll need to install it in our project. We can do this with the NPM CLI.
npm i xml-js
Or with the Yarn CLI:
yarn add xml-js
After installation, we’ll be able to import it into a JavaScript module, like this:
import { json2xml } from 'xml-js';
We use import destructuring to access the json2xml()
method directly from the library.
For a CommonJS module, we’ll import it like this instead:
const { json2xml } = require('xml-js');
The json2xml() function
The json2xml()
function from the library has two parameters. The first is the JSON string to convert to XML, and the second is an object.
const xml = json2xml(json, { compact: true, spaces: 4 });
Customize conversion of JSON to XML
This object is used to specify various options for customizing the conversion process.
In our example, we set the compact
property to true
to indicate that the JSON string input is in a compact form.
We set the spaces
property to 4
to indent nested XML nodes by 4 spaces. So we can reduce the indentation by setting spaces
to 1
:
import { json2xml } from 'xml-js';
const jsonObj = {
name: 'Garage',
cars: [
{ color: 'red', maxSpeed: 120, age: 2 },
{ color: 'blue', maxSpeed: 100, age: 3 },
{ color: 'green', maxSpeed: 130, age: 2 },
],
};
const json = JSON.stringify(jsonObj);
const xml = json2xml(json, { compact: true, spaces: 1 });
console.log(xml);
Now we will have the following XML output:
<name>Garage</name>
<cars>
<color>red</color>
<maxSpeed>120</maxSpeed>
<age>2</age>
</cars>
<cars>
<color>blue</color>
<maxSpeed>100</maxSpeed>
<age>3</age>
</cars>
<cars>
<color>green</color>
<maxSpeed>130</maxSpeed>
<age>2</age>
</cars>
Native conversion of JSON to XML
If you don’t want to use any third-party libraries, then you can use this recursive function to convert JSON to XML:
function JSONtoXML(obj) {
let xml = '';
for (let prop in obj) {
xml += obj[prop] instanceof Array ? '' : '<' + prop + '>';
if (obj[prop] instanceof Array) {
for (let array in obj[prop]) {
xml += '\n<' + prop + '>\n';
xml += JSONtoXML(new Object(obj[prop][array]));
xml += '</' + prop + '>';
}
} else if (typeof obj[prop] == 'object') {
xml += JSONtoXML(new Object(obj[prop]));
} else {
xml += obj[prop];
}
xml += obj[prop] instanceof Array ? '' : '</' + prop + '>\n';
}
xml = xml.replace(/<\/?[0-9]{1,}>/g, '');
return xml;
}
const jsonObj = {
name: 'Garage',
cars: [
{ color: 'red', maxSpeed: 120, age: 2 },
{ color: 'blue', maxSpeed: 100, age: 3 },
{ color: 'green', maxSpeed: 130, age: 2 },
],
};
const xml = JSONtoXML(jsonObj);
console.log(xml);
This code will produce the following output:
<name>Garage</name>
<cars>
<color>red</color>
<maxSpeed>120</maxSpeed>
<age>2</age>
</cars>
<cars>
<color>blue</color>
<maxSpeed>100</maxSpeed>
<age>3</age>
</cars>
<cars>
<color>green</color>
<maxSpeed>130</maxSpeed>
<age>2</age>
</cars>