How to Convert XML to JSON in JavaScript? Last Updated : 22 Oct, 2024 Comments Improve Suggest changes Like Article Like Report 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 the XML to JSON object.Table of ContentConvert XML to JSON using xml-js LibraryConvert XML to JSON using DOM ParserConvert XML to JSON using xml-js LibraryThe xml-js library converts the XML file to JSON in JavaScript. The xml2json function from the library takes XML data as input and converts it into a JSON object, with options like compact formatting and spacing specified.Run the below command to install xml-js Library:npm install xml-jsThe added dependency after installing xml-js is"dependencies": { "xml-js": "^1.6.11"}Example: The xml-js library converts the XML to JSON format with a compact representation and formatted with 2 spaces. JavaScript // index.js const convert = require('xml-js'); const xmlData = ` <data> <organization>GeeksforGeeks</organization> <founder>Sandeep Jain</founder> <location>Noida</location> </data> `; const jsonResult = convert.xml2json(xmlData, { compact: true, spaces: 2 }); console.log(jsonResult); Output{ "data": { "organization": { "_text": "GeeksforGeeks" }, "founder": { "_text": "Sandeep Jain" }, "location": { "_text": "Noida" } }}Convert XML to JSON using DOM ParserThe xmldom library's DOMParser is used to parse XML data in JavaScript. It traverse the parsed XML document's nodes to extract element names and their corresponding text content, storing them in a JSON object for conversion and formatting.Run the below command to install xmldom Library:npm install xmldomThe added dependency after installing xmldom is"dependencies": { "xmldom": "^0.6.0"}Example: Parses the given XML string to a JavaScript object using xmldom and then converts it to a formatted JSON string. JavaScript // index.js const { DOMParser } = require('xmldom'); const xmlString = ` <data> <organization>GeeksforGeeks</organization> <founder>Sandeep Jain</founder> <location>Noida</location> </data> `; 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(); } } const jsonResult = JSON.stringify(data, null, 2); console.log(jsonResult); Output{ "organization": "GeeksforGeeks", "founder": "Sandeep Jain", "location": "Noida"} Comment More infoAdvertise with us Next Article How to Convert XML to JSON in JavaScript? A anjalibo6rb0 Follow Improve Article Tags : JavaScript Web Technologies JSON Similar Reads 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 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 HTML to JSON in JavaScript ? Converting HTML to JSON is important for structured data extraction and integration with JavaScript applications. Here, we will learn different approaches to converting HTML to JSON in JavaScript. Below are the approaches to convert html to JSON in JavaScript: Table of Content Using html-to-json Lib 2 min read How to Convert JSON to Blob in JavaScript ? This article explores how to convert a JavaScript Object Notation (JSON) object into a Blob object in JavaScript. Blobs represent raw data, similar to files, and can be useful for various tasks like downloading or processing JSON data. What is JSON and Blob?JSON (JavaScript Object Notation): A light 2 min read How to Convert Excel to JSON in JavaScript ? Converting Excel spreadsheets to JSON format is a common requirement in various applications. JavaScript code utilizes the read-excel-file library to parse the Excel data, convert it to JSON format, and display it. Additionally, it provides functionality to download the generated JSON file. Approach 3 min read How to Convert JSON to Excel in JavaScript? It is often necessary to export or download JSON data in the form of Excel spreadsheets when developing web applications, any web developer would be able to go through this article as it provides a useful function of converting JSON files to Excel format using SheetsJS through JavaScript.These are t 4 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 Blob Data to JSON in JavaScript ? When dealing with Blob data in JavaScript, such as binary data or files, we may need to convert it into JSON format for doing so JavaScript provides us with various methods as listed below. Table of Content Using FileReader APIUsing TextDecoder APIUsing FileReader APIIn this approach, we first use t 2 min read How to Convert JSON Object to CSV in JavaScript ? JSON (JavaScript Object Notation) and CSV (Comma-Separated Values) are two widely used formats, each with its own strengths and applications. Fortunately, JavaScript provides powerful tools to facilitate the conversion process between these formats. These are the following approaches: Table of Conte 3 min read Like