About XML to JSON
Convert XML to JSON via the browser's DOMParser. Pick how attributes (<code>@key</code> by default) and text content (<code>#text</code> by default) appear in the output. Round-trips cleanly back to XML in most cases. Useful for processing legacy feeds, SOAP responses, and exported configs in JSON-native code.
When you need this
Modern services speak JSON. Legacy systems — SOAP services, RSS feeds, Atom feeds, OOXML, sitemaps, SVG metadata, Android resources — speak XML. Converting at the boundary is the cleanest way to keep the legacy data flowing into modern code without scattering XML parsing across your codebase.
What survives the conversion
- Element nesting → object nesting
- Repeated elements → JSON arrays
- Attributes →
@-prefixed keys - Text content →
#textkeys (or value-only when there is no mixed content) - Namespaces → preserved in element names
What loses fidelity
- Element order in mixed content
- Comments
- Processing instructions
- DOCTYPE
- Whitespace exactly as authored
For round-trippable data XML (RSS, Atom, configs), this is fine. For document XML (XHTML, DocBook), expect to lose layout.
Common workflows
Consume an RSS feed in JS. Fetch the XML, convert to JSON, iterate items array, render. No XML parsing in your application code.
Bridge a SOAP service to a REST front-end. SOAP responses are XML; convert at the gateway, hand JSON to the rest of the system.
Audit a sitemap. XML sitemaps convert to a JSON array of { loc, lastmod, priority } you can grep, filter, count.
Process Android resources. strings.xml, colors.xml convert to JSON for a build script that wants typed access.
Round-trip caveat
If you plan to convert back via JSON to XML (not yet available — use a library), preserve the attribute/text conventions. The converter assumes the same prefixes both directions; mismatching produces garbage.
Frequently asked questions
How are attributes mapped?
@ — so <item id="1"> becomes {"@id": "1"}. Toggle the prefix or pick "merge" to put attributes into the object directly (loses round-trip capability).What about mixed content?
<p>Hello <b>world</b></p> keeps text and elements separate via {"#text": "Hello ", "b": "world"}. Imperfect for complex documents but adequate for data-XML.Are namespaces preserved?
xmlns:foo="..." declarations stay; element names retain their prefix.Can I round-trip back?
How does it differ from yaml conversion?
Does it validate the XML first?
Related tools
Last updated: 2025-01-15