Convert xml data into json using Node.js - GeeksforGeeks

09 Apr, 2021
XML: Extensible Markup Language (XML) is a markup language that defines a set of rules for encoding documents in a format that is both human-readable and machine-readable. The design goals of XML focus on simplicity, generality, and usability across the Internet. It is a textual data format with strong support via Unicode for different human languages. Although the design of XML focuses on documents, the language is widely used for the representation of arbitrary data structures such as those used in web services.
Example:
<student>
    <details>
        <name> sravan</name>
        <id>1</id>
    </details>
    <details>
        <name> sudheer</name>
        <id>2</id>
    </details>
</student>
JSON: JSON stands for JavaScript Object Notation. It is a text-based data interchange format to maintain the structure of the data. JSON is the replacement of the XML data exchange format in JSON. It is easy to struct the data compare to XML. It supports data structures like arrays and objects and the JSON documents that are rapidly executed on the server. It is also a Language-Independent format that is derived from JavaScript. The official media type for the JSON is application/json and to save those file .json extension.
Example:
{"student":[
 { "Name":"sravan", "id":1 },
  { "Name":"sudheer", "id":2}
]}
Similarities:
JSON and XML are “self describing” nature
JSON and XML are hierarchical
We can parse JSON and XML
We can include XAML along with JSON and vive versa.
Differences:
JSON doesn’t use end tag whereas xml used end tag
JSON is shorter than xml
JSON is quicker to read and write
JSON can use arrays but xml can’t use arrays.
Approach:
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 system
xml2js used to convert XML to JSONfs stands for file system which is used to locate our local file system
xml2js used to convert XML to JSON
fs stands for file system which is used to locate our local file system
String the results using JSON.stringify() method.Syntax:JSON.stringify(results)
Syntax:
JSON.stringify(results)
Install Modules:npm install fs xml2js
npm install fs xml2js
code1.js
// import File System Moduleimport fs from "fs";   // import xml2js Moduleimport { parseString } from "xml2js";   //xml datavar 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 dataparseString(xmldata, function (err, results) {  // parsing to jsonlet data = JSON.stringify(results)  // display the json dataconsole.log("results",data);});
node code1.js
Output:
Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML  course.
HTML and XML
JSON
Node.js-Methods
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Express.js express.Router() Function
JWT Authentication with Node.js
Express.js req.params Property
Mongoose Populate() Method
Difference between npm i and npm ci in Node.js
Roadmap to Become a Web Developer in 2022
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Convert a string to an integer in JavaScript