XML to JSON

Convert XML to JSON online instantly. Free browser-based XML to JSON converter with formatted output.

Runs locally in your browser
XML Input5 lines · 92 chars
Loading editor…
JSON Output0 lines · 0 chars
Loading editor…

What is XML to JSON?

An XML to JSON converter transforms XML documents into JSON objects for use in modern web APIs, JavaScript applications, and NoSQL databases. XML's tag-based hierarchical structure maps naturally to nested JSON objects, though conventions differ for attributes, text nodes, namespaces, and repeated elements. Understanding these mapping rules is essential when migrating legacy SOAP services, parsing RSS feeds, or integrating with systems that export XML configuration.

XML represents data with nested elements, attributes on opening tags, and text content between tags. JSON represents the same hierarchy with objects, arrays, and primitive values. The conversion challenge lies in ambiguity: XML attributes could become JSON properties or nested objects, repeated sibling tags should become arrays, and mixed content (text plus child elements) requires special handling. Our converter uses consistent, documented conventions so output is predictable.

Our converter uses the browser's DOMParser to parse well-formed XML, then walks the element tree to build a JSON representation. XML attributes are prefixed with @ (for example @id), text-only content becomes string values, repeated sibling tags automatically become JSON arrays, and elements with both text and children include a #text field for interleaved content. The result is pretty-printed JSON with two-space indentation.

This approach is ideal for quick inspection, prototyping API migrations, and debugging legacy XML integrations without installing command-line tools like xml2json, yq, or writing custom XSLT transforms. Paste SOAP responses, RSS feeds, SVG snippets, Android layout XML, or Maven POM files and get formatted JSON instantly for use in Postman, Node.js scripts, or browser DevTools.

Conversion runs entirely in your browser. Enterprise XML payloads, internal API responses, customer data exports, and confidential configuration files never upload to external servers. Copy or download the JSON output, then format it further with the JSON Formatter, validate with the JSON Validator, or explore structure with the JSON Viewer.

For the reverse direction or complementary workflows, use the XML Formatter to validate and prettify source XML before conversion, and the XML Escape tool when embedding converted data back into XML contexts. Backend developers, integration engineers, and frontend developers use XML to JSON conversion daily when modernizing legacy systems and building API bridges.

When should you use XML to JSON?

Migrate legacy SOAP or XML-RPC integrations to REST APIs that consume JSON payloads in modern microservice architectures.

Inspect RSS, Atom, SVG, or Android layout XML structures as JSON trees during frontend and mobile development.

Convert Maven POM metadata, Spring configuration XML, or enterprise config files to JSON for documentation generators.

Debug XML API responses by viewing them in a familiar JSON format before writing parsing code in JavaScript or Python.

How to Use XML to JSON

  1. Paste XML into the input editor or upload an .xml file.
  2. Click Convert to transform XML into JSON.
  3. Review the formatted JSON output.
  4. Copy or download the JSON result.

Features

  • Convert well-formed XML to formatted JSON instantly
  • Attributes mapped as @attributeName keys for clear distinction
  • Repeated sibling elements become JSON arrays automatically
  • Pretty-printed JSON output with 2-space indentation
  • Upload .xml files via drag-and-drop
  • Copy or download JSON output with one click
  • 100% client-side processing — data stays private
  • Clear error messages for malformed or invalid XML

Example

The root element book becomes the top-level JSON key. The id attribute maps to @id, keeping attributes visually distinct from child element properties. Child elements author and title become string properties with their text content as values. This convention produces valid, readable JSON that JavaScript applications can parse directly with JSON.parse after minor key normalization if needed.

XML input

<book id="bk101"><author>Gambardella</author><title>XML Guide</title></book>

JSON output

{
  "book": {
    "@id": "bk101",
    "author": "Gambardella",
    "title": "XML Guide"
  }
}

Common Errors

Malformed XML

Unclosed tags, invalid characters, missing root elements, or wrong encoding declarations prevent parsing.

Fix: Use the [XML Formatter](/xml-formatter) to validate and fix structure before converting. Check file encoding.

Mixed content confusion

Elements containing both text and child elements produce #text alongside nested keys in the JSON output.

Fix: This is expected behavior. Review the #text field for interleaved text content between child tags.

Namespaces in tag names

Tags like ns:book or soap:Envelope include the namespace prefix in the JSON key name.

Fix: Strip namespace prefixes in source XML if you need cleaner keys, or post-process the JSON output.

Single versus repeated elements

A single occurrence becomes an object; multiple siblings become an array — inconsistent typing across documents.

Fix: Normalize output in application code to always expect arrays for known repeatable elements.

CDATA sections

CDATA blocks may appear as #cdata-section or plain text depending on parser behavior.

Fix: Inspect converted output for CDATA content; extract and process the text value as needed.

Best Practices

  • Validate XML well-formedness with the XML Formatter before conversion to avoid partial results
  • Document your @attribute convention when sharing converted JSON with teammates or API consumers
  • For production ETL pipelines, use schema-aware converters when strict type mapping is required
  • Compare converted output against your API contract before replacing XML parsers in production code
  • Convert locally when handling enterprise XML containing sensitive or regulated data
  • Normalize single-element arrays in application code for consistent downstream processing
  • Use the JSON Viewer to explore large converted documents as an interactive tree
  • Test round-trip fidelity if your workflow requires converting JSON back to XML later

Related Tools

Frequently Asked Questions

Attributes are mapped to keys prefixed with @, e.g. @id.