JSON to XML

Paste a JSON document and get back the XML equivalent with the name of the root element you choose, a consistent strategy for arrays (repeated elements or a wrapper), optional XML declaration and pretty indentation. Useful when a legacy system demands XML and your upstream data lives as JSON.

How to convert JSON to XML

  1. 1

    Paste JSON

    Object or array at the top level. You will give the root element a name.

  2. 2

    Name the root

    XML requires a single root element. Default is `<root>`; give it something meaningful like `<users>` or `<response>`.

  3. 3

    Pick array strategy

    "Repeated elements" (each array item becomes a sibling `<item>`) or "wrapper" (the array goes inside a named wrapper element).

  4. 4

    Copy the XML

    Optional XML declaration, proper indentation, and entity-escaped values for `<`, `>`, `&` and quotes.

Example: repeated elements

Input:

{ "users": [ { "name": "Alice" }, { "name": "Bob" } ] }

Output:

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <users>
    <name>Alice</name>
  </users>
  <users>
    <name>Bob</name>
  </users>
</root>

Example: array wrapper

Same input with the “wrap arrays in <item>” strategy:

<root>
  <users>
    <item>
      <name>Alice</name>
    </item>
    <item>
      <name>Bob</name>
    </item>
  </users>
</root>

Type mapping

JSON XML
string text content, entity-escaped
integer / decimal text content (no XML type annotation)
boolean true / false as text
null empty element (<foo/>) or omitted
array repeated elements or wrapper + items
object nested element with child elements

Attributes vs elements

XML distinguishes attributes (<user id="1">) from child elements (<user><id>1</id></user>). The converter defaults to elements, they are lossless for round-tripping. If your downstream XML requires attributes, use a key prefix convention:

  • JSON key @id becomes an XML attribute id="..." on the parent element.
  • JSON key #text becomes the text content of the parent element.

These conventions come from BadgerFish and Parker mappings, the two most common JSON-to-XML standards.

Escaping rules

  • < -> &lt;
  • > -> &gt;
  • & -> &amp;
  • " -> &quot; (in attribute values)
  • ' -> &apos; (optional)
  • Bytes in UTF-8 are preserved, declared in the XML prolog.

Common mistakes

  • Root-level JSON array. XML needs a single root element. The converter wraps an array in <root> (or whatever name you choose).
  • Numeric keys. JSON {"1": "a"} is legal; XML element names cannot start with a digit. The converter prefixes digits with an underscore (_1) or lets you define a custom mapping.
  • Mixed arrays. [1, "two", { "x": true }] has no clean mapping. Usually this means the JSON needs normalization before conversion.
  • Huge documents. XML is verbose, a 10 MB JSON can become a 20-30 MB XML. Consider streaming conversion for very large payloads.

Frequently Asked Questions

XML requires a single root by specification. If your JSON is an array or has multiple top-level keys, the converter wraps everything in a root element you can name (default <root>).

Yes. Use the @-prefix convention in your JSON keys (e.g. "@id": 1) and those keys become attributes. Other keys stay as child elements. This matches the BadgerFish mapping.

Two strategies: repeated sibling elements (each item gets the parent key as its tag), or a wrapper with <item> elements inside. Pick whichever matches the XML schema you are targeting.

No. The converter emits the XML declaration (<?xml version="1.0"?>) but not a DOCTYPE. For XML that needs a DTD or XSD reference, add it manually after conversion.

Related Tools

Tool available in other languages