To convert XML to JSON in JavaScript, you can use the xml2json
library:
var xmlString = '<book><title>The Alchemist</title><author>Paulo Coelho</author></book>';
// convert the XML string to a JSON string
var jsonString = xml2json.toJson(xmlString);
Code language: JavaScript (javascript)
In this code, the toJson()
method from the xml2json
library is used to convert the XML string to a JSON string.
To convert the JSON string to a JSON object you can then use JSON.parse
:
var jsonObject = JSON.parse(jsonString);
Code language: JavaScript (javascript)
Once you have the JSON object, you can access its properties and values using dot notation or bracket notation, just like with any other object in JavaScript. For example:
console.log(jsonObject.book.title); // logs "The Alchemist"
console.log(jsonObject.book.author); // logs "Paulo Coelho"
Code language: JavaScript (javascript)
JSON to XML
Above we talked about how to go from XML to JSON, now lets do the opposite.
To convert a JSON object to an XML string in JavaScript, you can use the xml2json
library in combination with the JSON.stringify()
method.
Next, you can convert a JSON object to an XML string using the following code:
var jsonObject = {
bookstore: {
book: [
{
title: "The Alchemist",
author: "Paulo Coelho"
},
{
title: "The Tao of Pooh",
author: "Benjamin Hoff"
}
]
}
};
// convert the JSON object to an XML string
var xmlString = xml2json.toXml(JSON.stringify(jsonObject));
Code language: JavaScript (javascript)
In this code, the JSON.stringify()
method is used to convert the JSON object to a JSON string. Then, the toXml()
method from the xml2json
library is used to convert the JSON string to an XML string.
The resulting XML string will look something like this:
<bookstore><book><title>The Alchemist</title><author>Paulo Coelho</author></book><book><title>The Tao of Pooh</title><author>Benjamin Hoff</author></book></bookstore>
Code language: HTML, XML (xml)
How to Use xml2json
To use the xml2json
library in your JavaScript code, you first need to install it using the npm install
command.
npm install xml2json
Once the library is installed, you can import it into your JavaScript code using the require()
function.
var xml2json = require('xml2json');
Code language: JavaScript (javascript)
Once the library is imported, you can use the toJson()
method to convert an XML string to a JSON string.
// import the xml2json library
var xml2json = require('xml2json');
// define the XML string to convert
var xmlString = '<bookstore><book><title>The Alchemist</title><author>Paulo Coelho</author></book><book><title>The Tao of Pooh</title><author>Benjamin Hoff</author></book></bookstore>';
// convert the XML string to a JSON object
var jsonObject = JSON.parse(xml2json.toJson(xmlString));
// log the JSON object to the console
console.log(jsonObject);
Code language: JavaScript (javascript)
In this example, the toJson()
method is used to convert the XML string to a JSON string, and the JSON.parse()
method is used to parse the JSON string and create a JSON object. The resulting JSON object is then logged to the console.
The output in the console will look something like this:
{
bookstore: {
book: [
{
title: "The Alchemist",
author: "Paulo Coelho"
},
{
title: "The Tao of Pooh",
author: "Benjamin Hoff"
}
]
}
}
Code language: JSON / JSON with Comments (json)
As you can see, the JSON object contains the same data as the XML string, but it is organized into a hierarchical structure using nested objects and arrays.
Leave a Reply