Skip to content
LocalOnly

XML to JSON Converter

Stable

Parse XML into predictable JSON with clear attribute and text mapping.

Everything is processed locally in your browser

About XML to JSON Converter

Convert XML documents into clean, predictable JSON without leaving your browser. This XML to JSON converter maps elements to keys, exposes attributes under an @ prefix, and places element text under a #text key so nothing is lost. It handles nested structures, repeated elements and mixed content, unescaping entities like & back into their real characters.

Features

  • Maps XML elements to JSON keys and nested elements to nested objects
  • Exposes attributes under an @ prefix so they never collide with elements
  • Stores element text under #text when a node also has attributes
  • Collapses repeated sibling tags into a JSON array automatically
  • Unescapes entities (&, <, >) back to real characters
  • Outputs pretty or minified JSON with correct handling of empty elements

How to use XML to JSON Converter

  1. 1

    Paste your XML

    Paste a well-formed XML document - a SOAP response, feed, or config file. The declaration and namespaces are parsed correctly.

  2. 2

    Convert to JSON

    The parser walks the XML tree, converting elements, attributes, and text into a consistent JSON shape.

  3. 3

    Copy the JSON

    Copy the resulting JSON or download it, then consume it in a modern app that prefers JSON over XML.

Examples

XML with attributes to JSON

Attributes map to @-prefixed keys; text with attributes uses #text.

Input

<catalog>
  <product sku="A100">
    <name>Wireless Mouse</name>
    <price currency="USD">24.99</price>
  </product>
</catalog>

Output

{
  "catalog": {
    "product": {
      "@sku": "A100",
      "name": "Wireless Mouse",
      "price": {
        "@currency": "USD",
        "#text": "24.99"
      }
    }
  }
}

How XML to JSON conversion works

Attributes are the first decision a converter has to make

XML has two ways to attach a value to an element - as an attribute or as a child element - and JSON has only one. So `<user id="42"><name>Ada</name></user>` presents an immediate problem: `id` and `name` are structurally different in XML but must both become object members in JSON.

The common resolution is to prefix attribute names, usually with `@`, giving `{"user":{"@id":"42","name":"Ada"}}`. This is lossless and lets a converter reconstruct the original XML, at the cost of keys that are awkward to work with in code. The alternative is to merge attributes in as ordinary members, which reads much better but loses the distinction and can collide when an attribute and a child element share a name.

Which you want depends on whether you need to convert back. For one-way consumption, merged attributes are more pleasant. For a round trip, prefixing is necessary.

The single-element array problem

This is the defect that causes real production incidents, so it is worth stating plainly. XML has no array type. A list of items is expressed as repeated sibling elements, which means `<items><item>a</item><item>b</item></items>` and `<items><item>a</item></items>` are structurally the same pattern with different counts.

A converter looking at the second one cannot tell whether `item` is a collection that happens to hold one element, or a single value. So it guesses - and the usual guess is that one element means a scalar. Your code then works perfectly against test data with two items and fails with a type error the day a response contains exactly one.

There are only two real defences. Either configure the converter with a list of element names that must always be treated as arrays, which requires knowing the schema, or normalise defensively in your own code: wrap anything you expect to be a collection in an array if it is not one already. The second is uglier and works without a schema, which is why it is what most production code ends up doing.

Mixed content and whitespace

XML allows text and elements to be siblings, which is natural for documents - `<p>Hello <b>world</b>!</p>` has text, an element, and more text as children of one node. JSON objects have no concept of ordered mixed members, so this cannot be represented directly. Converters typically collect the text into a special key such as `#text`, which preserves the content but loses the interleaving order. For document-oriented XML, that loss can be significant.

Whitespace is the related nuisance. XML preserves whitespace between elements by default, so pretty-printed XML contains text nodes made entirely of newlines and indentation. A converter that treats those as content produces `#text` keys full of whitespace throughout the output. Most trim them, which is almost always what you want - but it is a decision, and `xml:space="preserve"` exists precisely because sometimes the whitespace is data.

Reference

How XML constructs map to JSON

XMLJSONNote
Element with childrenObjectDirect
Repeated siblingsArrayAmbiguous when there is only one
Attribute"@name" keyPrefixed to avoid collisions
Text content"#text" keyOnly needed alongside attributes or children
Empty elementnull or empty stringConvention-dependent
xsi:nil="true"nullThe explicit null convention
Namespace prefixPart of the key namePrefix is kept, URI mapping is lost
CDATA sectionPlain stringContent preserved; the wrapper is not
CommentDroppedJSON has no comments
Mixed contentText collected in #textInterleaving order is lost

Which tool should you use?

These tasks overlap. Here is how to pick the right one for what you are actually doing.

You are consuming a SOAP or legacy XML API from JavaScript
This is the primary use. Normalise single-element collections defensively before your code relies on them.
You need to send XML back to the same system
Keep attribute prefixes so the JSON to XML tool can reconstruct the original structure.
The XML is document-oriented rather than data-oriented
Converting will lose the interleaving of mixed content. XSLT or an XML-aware parser is a better fit.
You just need a few values out of a large XML document
Convert, then use the JSON Path tool to extract them - often quicker than writing XPath.

Use cases

  • Consume a legacy SOAP or XML API response as JSON in a JS app
  • Convert an RSS or Atom feed into JSON for a custom reader
  • Migrate XML config files into a JSON-based configuration system
  • Inspect and query dense XML by turning it into readable JSON
  • Normalize third-party XML exports into JSON for a data pipeline

Troubleshooting common errors

A field is sometimes an object and sometimes an array

Why: The XML had one child element in some responses and several in others, and there is no way to tell a one-item list from a scalar.

Fix: Normalise in your code - coerce to an array if the value is not already one. This is the most important defensive habit when consuming converted XML.

Keys are prefixed with @ and are awkward to use

Why: They were XML attributes, and the prefix distinguishes them from child elements.

Fix: Keep the prefixes if you need to convert back; strip them in your own mapping layer if you do not.

The output is full of #text keys containing whitespace

Why: The XML was pretty-printed, and the indentation between elements is technically text content.

Fix: Enable whitespace trimming. Only disable it for documents where whitespace is genuinely significant.

Namespace prefixes clutter every key

Why: Prefixes such as `soap:` are part of the element name as far as the converter is concerned.

Fix: Strip the prefixes if you do not need them - but be aware that two namespaces can define the same local name, and stripping would then collide.

Numbers and booleans arrived as strings

Why: XML has no scalar types; all content is text unless an XSD declares otherwise.

Fix: Coerce in your code, or enable type inference and check the results on identifier fields.

Limitations

What this tool deliberately does not do, so you know when to reach for something else.

  • Single-element collections are indistinguishable from scalars without schema knowledge.
  • Mixed content loses the ordering of interleaved text and elements.
  • XML comments, processing instructions and the DOCTYPE are discarded.
  • Namespace URIs are not preserved, only the prefixes as written.
  • Type information is unavailable without an XSD, so values arrive as strings by default.

Frequently asked questions

Learn more

Command Palette

Search for a tool or command