🛠️DevTools

XML Attributes to JSON

XML attributes are prefixed with @ in JSON. Elements with both attributes and text use #text for the content.

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 = `<book id="1" category="fiction">
3 <title lang="en">The Great Gatsby</title>
4 <price currency="USD">12.99</price>
5</book>`;
6
7// Browser
8const parser = new DOMParser();
9const doc = parser.parseFromString(xmlString, 'text/xml');
10
11// Node.js with xml2js (npm install xml2js)
12const xml2js = require('xml2js');
13const parser = new xml2js.Parser();
14parser.parseString(xmlString, (err, result) => {
15 console.log(JSON.stringify(result, null, 2));
16});
17
18// Output:
19// {
20// "book": {
21// "@id": "1",
22// "@category": "fiction",
23// "title": {...

More XML Examples

Related XML Tools

📚 Learn More