Data

XML to JSON Converter

Convert simple XML into structured JSON with optional attribute support and text trimming.

Loading interactive tool…

Embed this tool on your site

About this tool

XML to JSON Converter turns XML into JSON in your browser using a consistent, attribute-aware mapping: element attributes go under an @attributes key, text content becomes a #text key, and repeated sibling elements become arrays. XML still powers SOAP APIs, RSS feeds, SVG files, Android resources, Maven POM files, and many legacy enterprise systems, and when you need that data in a JavaScript or Python application you need it as JSON. Converting by hand means dealing with attributes versus text nodes, self-closing tags, mixed content, namespaces, and CDATA sections, none of which map cleanly to JSON without a deliberate strategy. The result is a predictable, queryable structure you can work with immediately in JavaScript or jq.

Why use it

  • Attribute-Aware Mapping: XML attributes are kept in an @attributes object on their element rather than being silently dropped. This gives you access to IDs, types, and namespace declarations that live in attributes.

  • Repeated Element Arrays: Sibling elements with the same tag name are automatically grouped into a JSON array. This prevents the common mistake of losing data when multiple child elements share a tag and only the last one survives the conversion.

  • CDATA Handling: CDATA sections that contain escaped HTML or other character data are extracted and placed as string values in the JSON. The CDATA wrapper is removed cleanly without exposing its syntax in the output.

  • Namespace Aware: Prefixed element names such as soap:Body are kept as JSON keys, and xmlns declarations appear inside @attributes, so namespace-qualified elements from different vocabularies do not collide.

  • Plain Strings for Simple Elements: An element that contains only text and has no attributes or children is written as a plain string rather than a {#text: '...'} wrapper object, producing cleaner JSON for simple XML documents.

  • Browser-Local: Conversion runs entirely in your browser. SOAP request logs, Android resources, and legacy data exports never leave your machine or touch a server.

How to use

  1. Paste your XML document or fragment into the XML input box
  2. Leave 'Include attributes' checked to keep attributes under an @attributes key, or uncheck it to drop them
  3. Leave 'Trim text nodes' checked to strip surrounding whitespace from text, or uncheck it to keep it
  4. Click Convert to generate the JSON equivalent
  5. Review the output to confirm attribute handling and text nodes match your needs
  6. Click Copy and use the JSON with your JavaScript code, jq queries, or data pipeline

When it helps

  • You are consuming a SOAP API and need to parse the XML envelope response in JavaScript
  • You are processing an RSS or Atom feed and want a JSON structure you can iterate over
  • You are migrating data from a legacy XML-based export to a modern JSON-based system
  • You need to inspect an Android resource file or Maven POM as JSON in a query tool
  • You received an XML response from a legacy enterprise API and need to feed it into a REST pipeline
  • You want to use jq or JSONPath to query data that is currently locked in XML format

Examples

Simple element to object

Input
<user id="1"><name>Alice</name><role>admin</role></user>
Expected result
{
  "user": {
    "@attributes": {
      "id": "1"
    },
    "name": "Alice",
    "role": "admin"
  }
}

Repeated elements become array

Input
<items><item>A</item><item>B</item><item>C</item></items>
Expected result
{
  "items": {
    "item": [
      "A",
      "B",
      "C"
    ]
  }
}

CDATA section unwrapped

Input
<message><body><![CDATA[<b>Hello</b>]]></body></message>
Expected result
{
  "message": {
    "body": "<b>Hello</b>"
  }
}

Tips

  • After conversion, pipe the JSON through UtilityKit's JSON Formatter to validate structure and explore with folding.
  • If repeated elements are not becoming arrays, check whether the input truly has sibling elements or whether they are wrapped in a parent container.
  • Strip XML namespaces with a find-and-replace before converting if namespace prefixes create noisy key names in the output.
  • For SOAP envelopes, navigate directly to the Body element after conversion rather than parsing the full envelope structure.
  • Test the converter with a small representative sample before processing a large XML export to verify attribute and array handling.

Frequently Asked Questions

How are XML attributes represented in the JSON?
Attributes are collected into an @attributes object on the element. For example, <user id='1'> becomes {"user": {"@attributes": {"id": "1"}}}. Uncheck 'Include attributes' to leave them out entirely.
What happens when the same child element appears multiple times?
Repeated sibling elements with the same tag are grouped into a JSON array automatically. This prevents silent data loss from the last-value-wins problem that affects naive converters.
How are text nodes handled when an element has both text and children?
Mixed content — elements with both text and child elements — places the text in a #text key alongside the child element keys. With 'Trim text nodes' on, the text pieces are trimmed and joined with a space.
Does it handle XML namespaces?
Namespace prefixes are preserved in key names (soap:Body stays as "soap:Body") so that namespace-qualified elements do not collide. xmlns declarations are ordinary attributes, so they appear inside @attributes, for example "xmlns:soap".
Can it convert SVG files to JSON?
Yes. SVG is valid XML and the converter handles it correctly. However, SVG JSON is rarely the goal — this is most useful when you need to programmatically inspect SVG structure rather than render it.
How are CDATA sections handled?
CDATA sections are unwrapped and their content is placed as a plain string value. The CDATA delimiters (<![CDATA[ and ]]>) are removed from the output.
Are XML processing instructions and comments included?
No. Processing instructions and comments are left out of the JSON output. They carry no data semantics and would clutter the resulting structure.
Can I convert the JSON back to XML?
The conversion is lossy for some XML features, so round-tripping is not guaranteed to reproduce the original file exactly. For lossless XML processing, use an XML library or XSLT transformation.

Explore the category

Glossary

XML element
The fundamental building block of XML, consisting of an opening tag, optional content and child elements, and a closing tag. Elements form the hierarchical structure of an XML document.
XML attribute
A name-value pair placed inside an element's opening tag that provides metadata about the element. Attributes are distinct from child elements and are preserved as @attr keys in the JSON output.
CDATA section
A special XML syntax (<![CDATA[...]]>) that tells parsers to treat the enclosed content as raw character data, not markup. Commonly used to embed HTML or other XML-like content without escaping.
Namespace
An XML mechanism for qualifying element and attribute names with a URI prefix to avoid naming collisions, typically written as prefix:localname. Used extensively in SOAP, XHTML, and SVG.
Mixed content
An XML element that contains both text and child elements. Mixed content requires special handling during conversion since JSON objects cannot natively represent an ordered mix of text and element nodes.
SOAP
Simple Object Access Protocol — an XML-based messaging protocol used in enterprise web services. SOAP responses are XML documents with a defined Envelope/Body structure.
A LITTLE HELP. A LOT DONE.
Good tools.
Great flow.

Your shortcut to the everyday. Create, convert, calculate,
and get back to what you love.

✓ Free to use✓ No sign-up✓ 500+ possibilities