Blog
Wild & Free Tools

JavaScript XML to JSON: Node.js and Browser Examples

Last updated: January 2026 6 min read
Quick Answer

Table of Contents

  1. Node.js: xml2js
  2. Node.js: fast-xml-parser
  3. Browser: DOMParser API
  4. xml2js vs. fast-xml-parser vs. DOMParser
  5. Frequently Asked Questions

JavaScript has multiple approaches to XML-to-JSON conversion depending on where your code runs: Node.js has several well-maintained libraries (xml2js, fast-xml-parser), and the browser has the built-in DOMParser API. This guide covers all three with working examples and a comparison of their output format differences.

Node.js: Converting XML to JSON with xml2js

xml2js is the most-downloaded XML parsing library in the npm ecosystem. Install it:

npm install xml2js

Parse XML to a JavaScript object:

const xml2js = require('xml2js');

const xml = '<person><name>Alice</name><age>30</age></person>';

xml2js.parseString(xml, { explicitArray: false }, (err, result) => {

if (err) throw err;

console.log(JSON.stringify(result, null, 2));

});

The explicitArray: false option prevents xml2js from wrapping every value in an array — which is on by default and surprises many first-time users. Without it, <name>Alice</name> becomes "name": ["Alice"] instead of "name": "Alice".

For async/await style:

const { parseStringPromise } = require('xml2js');

const result = await parseStringPromise(xml, { explicitArray: false });

console.log(JSON.stringify(result, null, 2));

Node.js: fast-xml-parser — A Faster Alternative

fast-xml-parser is a newer library that benchmarks significantly faster than xml2js and has a more intuitive default output format:

npm install fast-xml-parser

const { XMLParser } = require('fast-xml-parser');

const parser = new XMLParser({ ignoreAttributes: false, attributeNamePrefix: '@_' });

const result = parser.parse(xmlString);

console.log(JSON.stringify(result, null, 2));

Key configuration options:

ignoreAttributes: false — includes XML attributes in the output (they're ignored by default)

attributeNamePrefix: '@_' — prefixes attribute keys with @_ (similar to the standard @ convention)

isArray: (name) => [...] — explicitly define which element names should always be treated as arrays (solves the single-item vs. array inconsistency)

fast-xml-parser is the better choice for performance-critical code or complex XML with many configuration requirements.

Sell Custom Apparel — We Handle Printing & Free Shipping

Browser JavaScript: DOMParser — No Library Needed

In browser JavaScript, the built-in DOMParser API parses XML without any npm package:

function xmlToJson(xmlString) {

const doc = new DOMParser().parseFromString(xmlString, 'application/xml');

const error = doc.querySelector('parsererror');

if (error) throw new Error('Invalid XML: ' + error.textContent);

return elementToObj(doc.documentElement);

}

function elementToObj(el) {

const obj = {};

for (const attr of el.attributes)

obj['@' + attr.name] = attr.value;

for (const child of el.children) {

const v = elementToObj(child);

if (obj[child.tagName]) {

if (!Array.isArray(obj[child.tagName])) obj[child.tagName] = [obj[child.tagName]];

obj[child.tagName].push(v);

} else obj[child.tagName] = v;

}

if (!el.children.length) return el.textContent.trim() || obj;

return obj;

}

This function handles attributes (@prefix), nested elements, repeated elements (arrays), and text content. It uses zero dependencies — just the browser's built-in XML parser.

xml2js vs. fast-xml-parser vs. DOMParser — Which to Use

xml2js: Best for existing Node.js projects already using it. Large ecosystem of examples and Stack Overflow answers. Default array behavior is confusing — always pass explicitArray: false.

fast-xml-parser: Best for new Node.js projects. Faster, more configurable, cleaner output. The modern choice for production XML processing.

DOMParser: Best for browser-side JavaScript. No library to install, works in all modern browsers, handles standard XML correctly. Requires a small custom function to convert the DOM to JSON.

For quick manual inspection (not automation): The browser converter converts any XML to JSON in your browser without writing a single line of code — paste XML, download JSON.

Skip the Code for Quick Conversions

Paste your XML and get clean JSON in seconds. No npm install, no setup.

Open Free XML to JSON Converter

Frequently Asked Questions

Why does xml2js wrap everything in arrays?

By default, xml2js wraps all element values in arrays to handle the case where an element might appear multiple times. Pass { explicitArray: false } to disable this for elements that appear once.

Can I use fast-xml-parser in the browser?

Yes. fast-xml-parser works in browser environments (via bundlers like webpack or Vite) as well as Node.js. It has no Node.js-specific dependencies.

What happens with XML namespaces in xml2js?

By default, xml2js preserves namespace prefixes as part of the key name. Use the { tagNameProcessors: [xml2js.processors.stripPrefix] } option to remove namespace prefixes from keys.

Is there a simpler way to convert XML to JSON without writing code?

Yes — the browser converter. Paste your XML, click convert, copy or download the JSON. No npm, no script writing, no setup.

Tyler Mason
Tyler Mason File Format & Converter Specialist

Tyler spent six years in IT support where file format conversion was a daily challenge.

More articles by Tyler →
Launch Your Own Clothing Brand — No Inventory, No Risk