A JSON to XML cheat sheet is a quick reference for the explicit mapping from JSON objects, arrays, scalars, and null to XML elements, attributes, and text.

JSON and XML do not share a universal one-to-one conversion standard. A converter must decide whether arrays become repeated elements or wrapped items, how null is represented, and whether JSON keys become elements or attributes. The rules below describe the documented typed mapping used by JSON To XML, not a promise that every API expects the same XML.

You can use the JSON To XML converter to apply this mapping in your browser. Processing stays inside the browser, and the pasted input is not uploaded.

json to xml cheat sheet
JSON to XML Cheat Sheet: Rules and Examples

JSON Values to XML Elements

The fastest way to use this cheat sheet is to identify the JSON value first, then look up its corresponding XML representation.

JSON value Typed XML representation Important detail
Object Element marked type="object" Properties become child elements or key-bearing item elements.
Array Parent element marked type="array" Every member becomes an ordered item child.
String Text inside an element marked type="string" XML-sensitive characters are escaped.
Number Text inside an element marked type="number" The value comes from the parsed JSON number.
Boolean Text inside an element marked type="boolean" The text is true or false.
Null Self-closing element marked type="null" Null remains distinct from an empty string.

The type attribute is what makes the mapping reversible in principle: a downstream reader can tell a numeric value from the same digits stored as a string, an array from an object, and null from an empty string. Empty objects and empty arrays also remain distinguishable through their type attributes.

Object properties follow JavaScript’s parsed property order. Array members retain their original order, which matters when position carries business meaning.

Names, Keys, and Escaping Rules

Use a root element name expected by the receiving system, then check whether every JSON key can be emitted directly as an XML element name.

  • Root name: Begin with an ASCII letter or underscore. Continue with ASCII letters, digits, underscores, periods, or hyphens.
  • Simple object key: A key that follows the same naming rule becomes an element name.
  • Reserved name: A name beginning with xml in any letter case is reserved. It is rejected as a root name and is not emitted directly as an element name.
  • Unsafe object key: A key that cannot be emitted directly becomes an item element, with the original key stored in an escaped key attribute.
  • Special characters: Ampersands, less-than signs, and greater-than signs are escaped in text. Double quotes are additionally escaped inside key attributes.

Escaping also prevents pasted strings from becoming XML structure. A JSON string containing something that looks like a tag remains text; the converter does not insert pasted content as executable or structural markup.

Convert a JSON Payload in Four Steps

Use this sequence when the typed output is compatible with the destination’s requirements.

  1. Paste valid JSON and name the root. Enter an object, array, or primitive JSON value, then provide a simple root element name that meets the naming rules.
  2. Select the output layout. Choose pretty printed for readable, two-space-indented XML or compact for XML without formatting whitespace.
  3. Run the conversion. Select Convert to XML to generate the document using the typed mapping described above.
  4. Review, copy, and validate. Inspect the type attributes and any escaped keys, copy the XML, and test it against the receiving system’s actual mapping or schema.

Invalid JSON, an invalid root name, and a reserved xml-prefixed root name produce errors before output. The converter itself does not validate the result against an XSD or an application-specific schema.

Typed Mapping Example

Consider this small object containing a number, a boolean, an array, and null:

Input JSON: {"id":42,"active":true,"tags":["new","sale"],"note":null}

Compact result: <?xml version="1.0" encoding="UTF-8"?><root type="object"><id type="number">42</id><active type="boolean">true</active><tags type="array"><item type="string">new</item><item type="string">sale</item></tags><note type="null"/></root>

The example illustrates the main conversion rules:

  • The root is marked as an object.
  • The id value is marked as a number rather than a string.
  • The tags array keeps its two members in order under item children.
  • The note is a self-closing null element, not an empty string.

The XML keys in this example are already safe, so none needs a key attribute. A key containing spaces, unsupported punctuation, or a reserved xml prefix would use the fallback item-and-key representation instead.

Pretty and Compact Output

The output mode changes readability, not the mapped data.

Mode Output behavior Typical use
Pretty printed Uses two-space indentation and line breaks. Inspection, review, and debugging.
Compact Removes formatting whitespace. Pasting or transmitting the generated XML.

Both modes retain the same elements, attributes, order, and values. The generated document also includes a UTF-8 XML declaration. Well-formed XML follows the syntax defined by the W3C XML 1.0 specification, but well-formedness alone does not guarantee that an API, schema, or vendor-specific convention will accept the document.

Number formatting follows browser JSON parsing rather than preserving the original numeric lexeme. The ECMA-262 JSON.parse specification describes the parsing stage; in practical terms, insignificant details such as trailing decimal zeroes are not retained after parsing.

Check the Receiving System

A documented conversion is still only one side of an integration. The receiving system may require a different root, repeated sibling tags, key-derived attributes, or its own null convention.

  • Validate the source JSON before conversion so syntax errors are not confused with mapping problems.
  • Confirm that the receiving system expects the selected root element name.
  • Convert a representative sample containing every relevant JSON type.
  • Parse the generated XML with the target system and compare its interpretation with the original values.
  • Expand to a larger dataset only after the sample passes.

Pay particular attention to arrays, null, empty containers, property order, and keys that need the escaped key attribute. If the receiver follows another convention, follow its contract rather than assuming this generic output will match.

If round-trip fidelity is required, document the exact typed mapping on both sides or use a schema-controlled transformation. The type attributes provide enough information for a compatible reader, but they do not make incompatible conventions interchangeable.

Limits and Unsupported XML Features

Processing remains inside the browser and is bounded to 500,000 input characters and 64 nesting levels. The character limit bounds browser work, while the depth limit prevents excessively nested input from exhausting the call stack.

The typed mapping intentionally does not represent every XML feature. It does not provide:

  • XML namespaces.
  • Attributes sourced from JSON keys.
  • Comments or processing instructions.
  • CDATA sections or document type declarations.
  • Mixed text-and-element content.

The page does not upload or save the input, validate against XSD, or send the output to an endpoint. It produces well-formed XML under its documented mapping; schema validation and application-level compatibility remain separate checks that should be performed with the receiving system’s own tooling.

Related reading: How to Check a JSON to Zod Schema Result.

Related reading: How to Validate JSON Response in REST Assured.