Skip to content
TypeParser
All tools

XML to JSON

Convert XML to JSON.

beats codebeautify.org edge: Attribute prefix + configurable text key
XML
paste XML
Guide

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 → #text keys (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 / npmnpm i fast-xml-parser, then new XMLParser().parse(xml).

CLIyq -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?
By default, attributes become keys prefixed with @ — 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?
Yes. xmlns:foo="..." declarations stay; element names retain their prefix.
Can I round-trip back?
Mostly. With the default attribute and text key conventions and a single child per element, JSON → XML reconstructs identically. Mixed content and order-sensitive XML lose structure.
How does it differ from yaml conversion?
YAML-to-JSON is a 1-to-1 structural mapping; XML-to-JSON is lossy because XML carries info JSON does not have (attribute order, mixed content, comment positions).
Does it validate the XML first?
Yes — DOMParser raises a parse error if the input is malformed. You see the line and column before any conversion happens.
How do I convert XML to JSON in Python, Java, or C#?
Python: xmltodict.parse(xml_string) gives a dict you dump with json.dumps. Java: org.json's XML.toJSONObject(xml).toString(), or Jackson's XmlMapperObjectMapper. 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?
Use JSON for new APIs, web front-ends, and config — it is lighter, maps directly to objects, and every language parses it natively. Use XML when you need attributes, mixed content, namespaces, schema validation (XSD), or you are integrating with SOAP, RSS/Atom, or enterprise systems built around it. JSON wins on readability and payload size; XML wins on document structure and formal validation.

Related tools

Last updated: 2026-07-04