Convert XML to JSON using JavaScript Last Updated : 06 May, 2024 Comments Improve Suggest changes Like Article Like Report 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: Table of Content Using xml-js LibraryUsing DOM ParserUsing xml-js LibraryIn 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-jsExample: 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 ParserIn 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 xmldomExample: 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"} Comment More infoAdvertise with us Next Article Convert XML to JSON using JavaScript P premlatac87 Follow Improve Article Tags : JavaScript Web Technologies JSON 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 XML data into JSON using PHP ? In this article, we are going to see how to convert XML data into JSON format using PHP. Requirements: XAMPP Server Introduction: PHP stands for hypertext preprocessor, which is used to create dynamic web pages. It also parses the XML and JSON data. XML stands for an extensible markup language in wh 3 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 Read JSON File Using JavaScript JSON (JavaScript Object Notation) is a lightweight format used for storing and exchanging data. In JavaScript, there are multiple ways to read and parse JSON files. These methods can be used both in browser environments and in Node.js.1. Using the fetch() API The fetch() API retrieves JSON files asy 3 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 How to Convert a Map to JSON String in JavaScript ? A Map is a collection of key-value pairs, where each key is unique. In this article, we will see how to convert a Map to a JSON (JavaScript Object Notation) string in JavaScript. However, JSON.stringify() does not directly support Map objects. Table of ContentUsing Object.fromEntries() MethodUsing A 2 min read How to Convert CSV to JSON in JavaScript ? In this article, we will explain different ways to change Comma-Separated Values (CSV) data into JavaScript Object Notation (JSON) format, step-by-step. We'll break down each method with clear explanations and examples. There are several approaches available in JavaScript to convert CSV to JSON in J 3 min read How to convert an object to string using JavaScript ? To convert an object to string using JavaScript we can use the available methods like string constructor, concatenation operator etc. Let's first create a JavaScript object. JavaScript // Input object let obj_to_str = { name: "GeeksForGeeks", city: "Noida", contact: 2488 }; Examp 4 min read Like