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 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 a JSON String into an SQL Query? JSON to SQL conversion is complicated because both have different structure. but this the help of the article you can easily convert JSON into SQL. We will use JavaScript language for conversion. Approach: Using JavaScript Validate JSON Format.Parse the JSON String into a JSON ObjectExtract Values f 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 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 Update Data in JSON File using Node? To update data in JSON file using Node.js, we can use the require module to directly import the JSON file. Updating data in a JSON file involves reading the file and then making the necessary changes to the JSON object and writing the updated data back to the file. Table of ContentUsing require() Me 4 min read Like