XML ↔ JSON Converter

Convert XML documents to JSON and JSON back to XML.

Indent:
XML INPUT
JSON OUTPUT

When an XML ↔ JSON converter is the right tool

The same record often exists as XML in a feed or a SOAP envelope and as JSON in an API. This page is for moving between those shapes in the browser. Choose a direction, paste the source, set 2- or 4-space indent, and click Convert. It is not a live converter: nothing is written to the output pane until that button is used.

If you only need to tidy one format, use the XML Formatter or the JSON Formatter instead.

Use cases

Turning a bookstore-style XML feed into JSON

Load Sample in XML → JSON pastes a short catalog with two books, attributes on book and title, and numeric year and price fields. After Convert, those attributes show up with an @_ prefix, and the two book elements become an array.

Building XML from a JSON tree

Switch to JSON → XML (this clears the input), paste an object that uses the same @_ attribute names, and click Convert. Load Sample in this direction pastes a smaller one-book tree. The builder pretty-prints with the indent you selected.

A cautious round trip

After a successful conversion, Swap puts the JSON or XML into the input and flips the direction; click Convert again to go back. Comments will not return, a single child may not stay an array, and numbers may no longer be quoted strings.

How conversion works

XML → JSON constructs an XMLParser with attributes kept and prefixed by @_, then pretty-prints with JSON.stringify. JSON → XML parses with JSON.parse and builds XML with XMLBuilder using the same attribute prefix and the selected indent. Errors from those steps are shown in the output pane.

Example — attributes and text become @_ and #text
<title lang="en">The Great Gatsby</title>

{
  "title": {
    "#text": "The Great Gatsby",
    "@_lang": "en"
  }
}

A bare text element with no attributes would appear as a string, not as an object with #text.

Example — a single child stays an object, not an array
<bookstore>
  <book><title>A</title></book>
</bookstore>

{
  "bookstore": {
    "book": {
      "title": "A"
    }
  }
}

Add a second <book> and book becomes an array of two objects. Code that assumes an array should account for the one-child case.

What this tool does not do

It does not convert as you type, preserve comments, or keep every single child as an array. It does not validate XML. Nothing is sent to a server. To compare two XML files, use XML Diff.

How to use XML ↔ JSON Converter

  1. Choose XML → JSON or JSON → XML. Switching tabs clears the input.
  2. Paste the source, or use Load Sample for a short example in the current direction. Pick indent 2 or 4.
  3. Click Convert, then copy the result if it looks right. Swap plus Convert runs the reverse direction on the current output.

Frequently asked questions

Is conversion live as I type?

No. You click Convert to run it. Changing indent after a result does not rewrite the output until you convert again. Switching XML → JSON or JSON → XML clears the input and the output. Swap moves the current output into the input, flips the direction, and clears the output, so you still need to click Convert to go back.

How are XML attributes and text represented in JSON?

The page uses fast-xml-parser with ignoreAttributes off and an @_ prefix. An attribute lang becomes @_lang. When an element has both attributes and character data, the text is stored under #text. A declaration such as <?xml version="1.0"?> becomes a ?xml object in the JSON and can be built back into a declaration on the way back.

When does an element become a JSON array?

Sibling elements that share a name become an array. A name that appears only once is a single object or value, not a one-element array. If you convert a file with one <book> and later add a second, the JSON shape changes from an object to an array. Numbers such as 1925 or 9.99 are parsed as numbers when the parser can do so.

Are comments, CDATA, and invalid XML preserved?

Comments are dropped. CDATA is typically merged into text (the parser may put it in #text). The parser is also tolerant: some malformed markup is still turned into a JSON tree instead of failing. Do not treat a successful conversion as proof that the XML is well-formed.

Is my data sent to a server?

No. Parsing and building run in your browser when you click Convert. Closing the tab discards what you pasted. Do not paste documents that contain secrets if you would not be comfortable with them sitting in memory on this device.

Is there a file size limit?

There is no server-imposed limit, because nothing is uploaded. A few thousand lines is comfortable. A very large document can make the tab feel slow; in that case, convert a smaller excerpt or use a local tool.

Related tools and guides

References

  • fast-xml-parser is the library used for both directions. Attribute names are prefixed with @_, and mixed content uses #text.
  • W3C XML 1.0 defines well-formedness. A completed conversion on this page does not mean the original XML passed that test.