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.
How to convert XML to JSON in code
Preview the mapping here, then automate:
Python (pip install xmltodict):
import xmltodict, json
doc = xmltodict.parse(open("in.xml").read())
json.dump(doc, open("out.json", "w"), indent=2)
Java (org.json):
import org.json.XML;
String json = XML.toJSONObject(xmlString).toString(2);
C# (Newtonsoft):
var doc = new XmlDocument(); doc.LoadXml(xml);
string json = JsonConvert.SerializeXmlNode(doc);
Node / npm — npm i fast-xml-parser, then new XMLParser().parse(xml).
CLI — yq -p=xml -o=json '.' in.xml, or xmlstarlet piped into jq.
Spring Boot / REST API — register both MappingJackson2HttpMessageConverter and MappingJackson2XmlHttpMessageConverter; content negotiation then returns JSON or XML from the same endpoint based on the Accept header. Power Automate has a native Compose + json(xml(...)) expression for the same conversion inside a flow.
The recurring gotcha is attribute and mixed-content handling — every library encodes them differently. Paste your XML above to see exactly how @-attributes and #text nodes land before you pick a library.
Round-trip caveat
If you plan to convert back to XML, 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?
How do I convert XML to JSON in Python, Java, or C#?
xmltodict.parse(xml_string) gives a dict you dump with json.dumps. Java: org.json's XML.toJSONObject(xml).toString(), or Jackson's XmlMapper → ObjectMapper. C#: JsonConvert.SerializeXmlNode(doc) from Newtonsoft. On the command line, xmlstarlet plus jq, or yq -p=xml -o=json. This tool matches the attribute/text conventions so you can preview output before coding.When should I use XML vs JSON?
Related tools
Last updated: 2026-07-04