· 4 min read
Convert XML to JSON Without Writing a Parser
Heshan Fernando
Co-founder & COO
You’ve pulled a response from a legacy SOAP API, or exported a config file from an old system, and it’s XML — angle brackets, attributes, nested elements three levels deep. The frontend code you’re writing wants JSON. Writing a proper XML parser for a one-off conversion is a waste of an afternoon, and regex-splitting tags by hand breaks the moment you hit a self-closing element or an attribute with a colon in it.
XML and JSON aren’t a clean 1-to-1 mapping either, which is exactly why people get stuck. JSON has no concept of an “attribute” versus a “child element” — XML has both, and a naive converter has to make a decision about how to represent that difference, or it silently drops data.
What converting XML to JSON actually involves
Parsing XML means walking its tree structure — elements, nested elements, attributes, and text content — and deciding how each maps to JSON’s simpler shape of objects, arrays, strings, and numbers. Attributes usually become object keys prefixed somehow (like @id), repeated sibling elements need to become an array instead of overwriting each other, and empty or self-closing tags need a sensible default rather than throwing an error.
Do it with a real parser — the same one browsers use to render XML — and you get correct handling of edge cases like CDATA sections, namespaces, and escaped entities for free. Do it with string splitting and you get something that works on your test file and breaks on the next one.
Why people get stuck here
- Attributes vs. elements.
<user id="42">Alice</user>has both an attribute and text content, and naive converters often lose one or the other. - Repeated tags.
<item>A</item><item>B</item>under the same parent needs to become a JSON array, not two competing values for one key. - Malformed XML. A missing closing tag or an unescaped
&breaks strict parsers, and the error message from a bad regex-based converter is rarely helpful. - No feedback until it’s wrong. Copy-pasting XML into a black-box converter and getting JSON back with no way to see what went wrong mid-conversion.
What a good XML-to-JSON converter looks like
A real XML parser under the hood
Using the browser’s native XML parser (rather than regex or a hand-rolled string splitter) means CDATA, entities, and nesting are handled the way a browser actually interprets XML — not approximated.
Live conversion as you type or paste
Seeing the JSON update immediately, rather than clicking “convert” and hoping, makes it much easier to catch a structural surprise (like an attribute that got flattened wrong) before you build code around it.
Clear handling of edge cases
Self-closing tags, empty elements, and mixed content (text plus child elements) should produce predictable, documented output — not silently vanish.
Common mistakes to avoid
- Assuming every XML-to-JSON tool produces the same JSON shape — attribute handling in particular varies a lot between converters.
- Skipping validation on malformed XML and wondering why the output JSON looks truncated.
- Losing numeric or boolean typing — most converters output everything as a string, so
"42"isn’t the same as42in downstream code that expects a number. - Converting a huge XML export (tens of MB) in a tool built for small config snippets, and hitting performance limits.
- Forgetting that XML namespaces (
xmlns:ns="...") often show up as literal prefixes in the converted keys, not stripped automatically.
How to do it with XML to JSON Converter
Online Tool Store’s XML to JSON Converter uses the browser’s own XML parser and updates the output live — nothing is sent to a server.
- Open the tool and paste or type your XML.
- Watch the JSON output update as you edit, so you catch structural issues immediately.
- Check how attributes and repeated elements were represented in the result.
- Copy the JSON output straight into your code or config file.
Because it runs entirely client-side, it’s a reasonable choice even for XML pulled from an internal system you’d rather not paste into a random web form.
Frequently asked questions
How are XML attributes represented in the JSON output?
Attributes typically appear as separate keys on the resulting object (often prefixed, like @id), distinct from the element’s text content or child elements — so you don’t lose the difference between the two.
What happens with repeated sibling tags?
Repeated elements under the same parent become a JSON array, preserving order, rather than overwriting each other as a single key would.
Can it handle malformed XML?
A parser built on the browser’s native XML engine will flag genuinely broken XML (like an unclosed tag) rather than guessing — fix the source XML first if the output looks wrong or incomplete.
Final thought
For small-to-medium XML — API responses, config exports, RSS-style feeds — a browser-based converter that uses a real parser will save you more time than writing a one-off script, and it won’t quietly mishandle an attribute the way a regex approach can.