🛠️DevTools

Nested XML to JSON

Nested XML elements become nested JSON objects. Multiple elements with the same name become arrays.

Switch tool:
Examples:
JSON output will appear here...

💡 Paste your XML and it converts to JSON automatically. Attributes are prefixed with @ and text content uses #text.

Code Examples

Here's how to achieve this in different programming languages:

1// Using DOMParser (browser) or xml2js (Node.js)
2const xmlString = `<company>
3 <department>
4 <name>Engineering</name>
5 <employees>
6 <employee>John</employee>
7 <employee>Jane</employee>
8 </employees>
9 </department>
10</company>`;
11
12// Browser
13const parser = new DOMParser();
14const doc = parser.parseFromString(xmlString, 'text/xml');
15
16// Node.js with xml2js (npm install xml2js)
17const xml2js = require('xml2js');
18const parser = new xml2js.Parser();
19parser.parseString(xmlString, (err, result) => {
20 console.log(JSON.stringify(result, null, 2));
21});
22
23// Output:
24// {
25// "company": {
26// "department": {
27// "name": "Engineering",
28// "employees": {...

More XML Examples

Related XML Tools

📚 Learn More