XML to JSON

XML and JSON model data differently: XML has attributes and mixed content, JSON has objects and arrays. Converting without loss needs a convention. This tool offers three: compact (drops attributes), Badgerfish (attributes under @key, text under $) and literal (every XML node becomes a JSON object with full fidelity). Pick the one that matches the JSON consumer you are targeting.

How to convert XML to JSON

  1. 1

    Paste XML

    Any well-formed XML. Namespaces are preserved.

  2. 2

    Pick conversion style

    Compact, Badgerfish or literal/lossless.

  3. 3

    Handle arrays

    Force certain elements to always be arrays (configurable).

  4. 4

    Copy JSON output

    Pretty-printed or minified, your choice.

The three styles compared

Input:

<book id="1" lang="en">
  <title>1984</title>
  <author>Orwell</author>
</book>

Compact (drops attributes):

{
  "book": {
    "title": "1984",
    "author": "Orwell"
  }
}

Badgerfish (attributes under @, text under $):

{
  "book": {
    "@id": "1",
    "@lang": "en",
    "title": { "$": "1984" },
    "author": { "$": "Orwell" }
  }
}

Literal / JsonML-ish (full fidelity):

{
  "book": {
    "@attributes": { "id": "1", "lang": "en" },
    "title": "1984",
    "author": "Orwell"
  }
}

Forced arrays

XML has no way to say “this is a list of one”. A single-element sequence looks identical to a scalar:

<items>
  <item>A</item>
</items>

That can serialise as either {"item": "A"} or {"item": ["A"]}. The converter lets you mark element names (by XPath) as always-arrays, so your downstream JSON consumer gets consistent types.

Mixed content

Mixed content, text interleaved with child elements, is the one place where any XML-to-JSON conversion loses information:

<p>Hello <em>world</em>!</p>

Most styles either drop text or collapse it. The literal style preserves order via an array:

{
  "p": {
    "@content": ["Hello ", { "em": "world" }, "!"]
  }
}

Type inference

By default values are strings. Toggle “parse numeric/boolean” and the converter will cast 42, 3.14, true, false to the matching JSON types. Be careful: an XML attribute id="007" becomes 7 with casting on, which you probably do not want.

When to pick which

  • Compact: for human-readable JSON where attributes are noise.
  • Badgerfish: when you need attributes and text at the same depth as children.
  • Literal: when you must round-trip back to XML without loss.

Frequently Asked Questions

When an element appears once, the converter defaults to object; twice or more, array. Use the force-array option to make it consistent if your consumer expects one shape.

Namespace prefixes are preserved in keys (ns:title) unless you enable “strip namespaces”. The declarations themselves become @xmlns:ns attributes on the root element.

Literal mode is the closest to lossless but mixed content ordering between elements and text is tricky. Compact mode definitely loses attributes. Badgerfish is a pragmatic middle ground.

No. Conversion runs in your browser.

Related Tools

Tool available in other languages