Convert XML data into JSON using Node.js Last Updated : 16 Oct, 2024 Comments Improve Suggest changes Like Article Like Report 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. First, parse the XML string, then transform it into JSON format, enabling easier manipulation of the data.Steps to Convert XML data to JSON using NodeStep 1: Install Modules:npm install xml2jsThe updated dependencies in the package.json file will be:"dependencies": { "xml2js": "^0.4.23",}Step 2: We need to import xml2js and fs module. xml2js used to convert XML to JSONfs stands for file system which is used to locate our local file systemimport { parseString } from "xml2js"; Step 3: String the results using JSON.stringify() method. Syntax:JSON.stringify(results)Example: This example converts the xml input data to json data using xml2js module. JavaScript // Filename - app.js // import File System Module import fs from "fs"; // import xml2js Module import { parseString } from "xml2js"; //xml data var xmldata = '<?xml version=”1.0" encoding=”UTF-8"?>' + '<Student>' + '<PersonalInformation>' + '<FirstName>Sravan</FirstName>' + '<LastName>Kumar</LastName>' + '<Gender>Male</Gender>' + '</PersonalInformation>' + '<PersonalInformation>' + '<FirstName>Sudheer</FirstName>' + '<LastName>Bandlamudi</LastName>' + '<Gender>Male</Gender>' + '</PersonalInformation>' + '</Student>'; // parsing xml data parseString(xmldata, function (err, results) { // parsing to json let data = JSON.stringify(results) // display the json data console.log("results",data); }); node code1.jsOutput: Comment More infoAdvertise with us Next Article Convert XML data into JSON using Node.js sravankumar_171fa07058 Follow Improve Article Tags : Web Technologies Node.js HTML and XML JSON Node.js-Methods +1 More Similar Reads Convert XML to JSON using JavaScript 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 2 min read How to Add Data in JSON File using Node.js ? JSON stands for Javascript Object Notation. It is one of the easiest ways to exchange information between applications and is generally used by websites/APIs to communicate. For getting started with Node.js, refer this article.Prerequisites:NPM NodeApproachTo add data in JSON file using the node js 4 min read Convert JSON file into CSV file and displaying the data using Node.js There are so many ways to store data for better understanding for individual purposes, in few cases, JSON files will be more suitable in few cases, CSV files, and there are also lots of other types as well like XML, etc. In this article, we will convert the JSON file data into CSV file data and also 3 min read 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 How to convert tabular string to JSON using Node.js ? Tabular: Information that is displayed in a table with rows and columns is known as "tabular format". A data table is an organized and practical approach to displaying a significant amount of information that contains repeated data items. The majority of office productivity software packages, includ 5 min read Like