You're wiring up a shiny new front end, and the shipping provider’s API answers in XML that looks like it was written in 2004. Probably because it was. Before you can use the data, you need it as JSON, and that conversion has a few surprises worth knowing about.
XML and JSON compared
Both formats describe structured data, in different ways. XML uses opening and closing tags, attributes and namespaces. JSON uses keys and values, with objects, arrays, strings, numbers, booleans and null.
XML was the main data format of the early 2000s. It's still everywhere in enterprise systems, SOAP web services, build files such as Maven’s pom.xml, and document formats like SVG. JSON is now the default for REST APIs and web apps, so you'll convert between the two whenever an older system meets a newer one.
Why XML to JSON isn't one-to-one
XML has features JSON simply doesn't have, such as attributes and repeated tags. So every converter has to pick a convention for them. Here are the ones the PulpMiner converter uses.
Attributes become @ keys
An XML element can have attributes (the id="123" bits inside the tag) as well as child elements. JSON only has keys, so attribute names get an @ prefix to keep them apart from child elements:
<book id="123" lang="en">
<title>Clean Code</title>
<author>Robert C. Martin</author>
</book>{
"book": {
"@id": "123",
"@lang": "en",
"title": "Clean Code",
"author": "Robert C. Martin"
}
}Text goes in #text
An element that holds only text becomes a plain string. If it also has attributes, the text is stored under a #text key:
<product>
<name>Pour-over coffee kit</name>
<price currency="USD">34.50</price>
</product>{
"product": {
"name": "Pour-over coffee kit",
"price": {
"@currency": "USD",
"#text": "34.50"
}
}
}One thing it can't do well is mixed content, where text and tags sit side by side, as in <p>This is <b>important</b> text.</p>. The tool keeps only part of that text. Mixed content is common in document-style XML such as XHTML, so for those files use a library that preserves node order.
Repeated elements become arrays
XML happily allows several sibling elements with the same name. A JSON object can't repeat a key, so repeated elements are collected into an array:
<library>
<book>Clean Code</book>
<book>The Pragmatic Programmer</book>
<book>Refactoring</book>
</library>{
"library": {
"book": [
"Clean Code",
"The Pragmatic Programmer",
"Refactoring"
]
}
}Here's the catch: a <library> with only one <book> gives you a plain string, not a one-item array. Code that reads the result should handle both shapes, or it'll break on the day someone owns only one book.
How to use the XML to JSON converter
- Open the XML to JSON converter and paste your XML.
- Select Convert. If the XML isn't well-formed (a missing closing tag is the usual culprit), you'll see the parser’s error message instead of output.
- Review the JSON and select Copy to put it on your clipboard.
Your browser’s built-in DOMParser does the parsing, so nothing is sent to a server. A few details: whitespace between elements is ignored, empty elements become an empty string, and comments are dropped. CDATA sections (<![CDATA[ ... ]]>blocks) are skipped too, so move their content into ordinary text first if you need it.
Converting XML API responses in Node.js
If an XML API is part of your app rather than a one-off, convert in code. Here's an example with the popular xml2js library:
const xml2js = require("xml2js");
const xml = `
<response>
<status>200</status>
<data>
<user id="1">
<name>Alice</name>
<email>alice@example.com</email>
</user>
</data>
</response>`;
xml2js.parseString(xml, { explicitArray: false }, (err, result) => {
if (err) throw err;
console.log(JSON.stringify(result, null, 2));
});By default xml2js puts attributes under a $ key rather than using @ prefixes, so its output won't match the online tool exactly. For a quick look at a response while debugging, pasting it into the converter is faster.
Common pitfalls
- Namespaces stay in the keys. You get keys such as
soap:Envelopeand attributes such as@xmlns:soap, which you may want to strip afterwards. - Order matters in XML, but code shouldn't rely on the order of keys in a JSON object. If order is meaningful, read it from arrays.
- Types don't exist in XML, so every value comes out as a string.
"34.50"stays text until you convert it.
When XML is still the better choice
XML isn't just legacy. It has schemas (XSD) for strict validation, namespaces to avoid naming clashes, and XSLT for transformations, which is why publishing, legal and medical systems still use it. For data exchange between web services and for most configuration, though, JSON is simpler, and JavaScript reads it natively.
