Convert XML to JSON using JavaScript
Last Updated :
06 May, 2024
Convert XML to JSON effortlessly using JavaScript, enabling seamless integration and manipulation of XML data within your applications. JavaScript libraries like xml-js and xmldom Library simplify the conversion process, ensuring compatibility and efficiency.
Below are the approaches to convert XML to JSON in JavaScript:
Using xml-js Library
In this approach, we are using the xml-js library to handle XML to JSON conversion in JavaScript. This library provides a convenient 'xml2json' function, which directly transforms XML data into a JSON object. It offers options for formatting, including compactness and spacing.
Run the below command to install xml-js Library:
npm install xml-js
Example: The below example uses xml-js Library to convert XML to JSON in JavaScript.
JavaScript
const convert = require('xml-js');
const xmlData = `
<book>
<title>The Great Gatsby</title>
<author>F. Scott Fitzgerald</author>
<genre>Fiction</genre>
</book>
`;
const jsonResult = convert.xml2json(xmlData, {
compact: true,
spaces: 2
});
console.log(jsonResult);
Output:
{
"book": {
"title": {
"_text": "The Great Gatsby"
},
"author": {
"_text": "F. Scott Fitzgerald"
},
"genre": {
"_text": "Fiction"
}
}
}
Using DOM Parser
In this approach, we are using the 'xmldom' library to handle XML parsing in JavaScript. This method involves parsing the XML data into a DOM structure and then traversing the nodes to extract element names and their corresponding text content. This method results in a JSON object representing the structure and content of the original XML data.
Run the below command to install xmldom Library:
npm install xmldom
Example: The below example uses DOM Parser to convert XML to JSON in JavaScript.
JavaScript
const { DOMParser } = require('xmldom');
const xmlString = `
<book>
<title>The Great Gatsby</title>
<author>F. Scott Fitzgerald</author>
<genre>Fiction</genre>
</book>
`;
const parser = new DOMParser();
const xmlDoc =
parser.parseFromString(xmlString, 'text/xml');
const data = {};
const nodes = xmlDoc.documentElement.childNodes;
for (let i = 0; i < nodes.length; i++) {
const node = nodes[i];
if (node.nodeType === 1) {
data[node.nodeName] = node.textContent.trim();
}
}
console.log(JSON.stringify(data, null, 2));
Output:
{
"title": "The Great Gatsby",
"author": "F. Scott Fitzgerald",
"genre": "Fiction"
}
Similar Reads
How to Convert XML to JSON in JavaScript? To convert XML to JSON in JavaScript, various methods and libraries and be used. Here, we use xml-js library that provides xml2json function to convert XML to JSON data. It takes XML data as input and gives the JSON objects as output. We can also use the DOMParser from the xmldom package to convert
2 min read
Convert XML data into JSON using Node.js Converting XML data to JSON in Node.js is a common task, especially when working with APIs or services that return data in XML format. This conversion allows you to manipulate the data more easily using JavaScript objects.ApproachTo convert XML to JSON in Node we will use libraries like xml2js. Firs
2 min read
How to Convert JSON to string in JavaScript ? In this article, we are going to learn the conversion of JSON to string in JavaScript. Converting JSON to a string in JavaScript means serializing a JavaScript object or data structure represented in JSON format into a textual JSON string for data storage or transmission.Several methods can be used
3 min read
How to Convert String to JSON in JavaScript? In JavaScript, converting a string to JSON is important for handling data interchangeably between server and client, parsing external API responses, and storing structured data in applications. Below are the approaches to converting string to JSON in JavaScript: Table of Content Using JSON.parse()Us
2 min read
How to Convert Map to JSON in JavaScript ? In JavaScript, when working with data, you might encounter situations where you need to convert a Map object into a JSON format. This can be useful for sending data over the network, storing data in local storage, or interfacing with APIs that expect JSON data. Converting a Map to JSON means convert
3 min read