XML to JSON converter

Convert XML to JSON or JSON to XML using a stated convention for attributes and text.

Runs in your browser. Your input is not uploaded.

What it does

XML and JSON do not map one-to-one: XML has attributes, mixed text and repeated elements; JSON has arrays and types. Any converter has to pick a convention. This one writes attributes as @name, text alongside other content as #text, and repeated child elements as arrays.

The reverse direction uses the same convention, so XML converted to JSON and back keeps its structure.

How to use it

  1. Choose the direction.
  2. Paste the XML or JSON.
  3. Copy the result.

Example

Input
<r><i id="1">a</i><i id="2">b</i></r>
Output
{
  "r": {
    "i": [
      { "@id": "1", "#text": "a" },
      { "@id": "2", "#text": "b" }
    ]
  }
}

Things to watch for

  • A single child element becomes an object, two become an array. Code that reads the JSON must handle both.
  • All XML values are strings. Numbers are not detected.
  • Comments and processing instructions are dropped.

Questions

Is there a standard for XML to JSON?
No. Conventions such as BadgerFish, Parker and the @/#text style used here all differ. Check what the consumer expects.
How are namespaces handled?
Prefixes are kept in the names, such as soap:Envelope. Namespace declarations appear as @xmlns attributes.
What root name is used for JSON to XML?
If the JSON has a single top-level key, that key is the root. Otherwise the output is wrapped in <root>.
Does it validate the XML?
It must be well-formed to convert. Errors show the line and column.