Xerobit

JSON to XML Converter

Use this json to xml converter to transform JSON data into well-formed XML instantly. You can set a custom root element name, choose whether to include the XML declaration, and control indentation. The conversion runs entirely in your browser, so nothing is uploaded to any server.

XML Output

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

How to use the json to xml converter

  1. Paste or type your JSON into the input area.
  2. Set a root element name if you need something other than the default root.
  3. Toggle the XML declaration on or off depending on your target system.
  4. Adjust the indentation spaces for readability, or set it to 0 for compact output.
  5. Copy the resulting XML from the output area.

How the json to xml conversion works

The converter parses your input with JSON.parse and walks the resulting structure recursively. Each JSON object key becomes an XML element name. String, number and boolean values become text content inside that element. Arrays produce repeated sibling elements using the parent key as the tag name. Null values render as self-closing empty elements.

For example, the JSON {"user": {"name": "Ada", "scores": [95, 100]}} with a root of data produces:

<data><user><name>Ada</name><scores>95</scores><scores>100</scores></user></data>

Key order is preserved exactly as it appears in the parsed JSON, giving you deterministic output every time.

Element name sanitization

XML element names must start with a letter or underscore and may only contain letters, digits, hyphens, underscores and periods. When a JSON key violates these rules, the converter replaces invalid characters with underscores and prepends an underscore if the name starts with a digit. If the key is completely empty, it falls back to _.

Whenever one or more keys are changed, a note appears below the output listing each original key alongside its sanitized replacement. This keeps you informed without silently altering your schema.

Escaping special characters

Text content in XML must escape five characters: & becomes &amp;, < becomes &lt;, > becomes &gt;, " becomes &quot; and ' becomes &apos;. The converter applies these replacements to all string values automatically.

Unicode characters, including emoji and non-Latin scripts, pass through unchanged because XML natively supports UTF-8. The optional XML declaration explicitly states encoding="UTF-8" to signal this to parsers.

Common mapping patterns

JSON typeXML result
Object {"a": 1}<a>1</a>
Array [1, 2]Repeated elements: <item>1</item><item>2</item>
String "hi"Text node: <key>hi</key>
Number 42Text node: <key>42</key>
Boolean trueText node: <key>true</key>
Null nullSelf-closing: <key/>

When the top-level JSON value is a primitive (string, number, boolean or null) rather than an object or array, it is placed directly inside the root element as text content or as an empty element.

Practical use cases

Many legacy APIs and enterprise systems still expect XML payloads even when modern services produce JSON. Converting between the two formats is a routine task when integrating newer microservices with older SOAP endpoints or configuration systems.

If you also need to present JSON data visually, tools like json to html table or json to markdown table handle tabular rendering. For generating typed models from JSON, see the converters for Java, C#, Go, Python or Kotlin.

Worked examples

ExampleInputResult
Simple objectJSON input: {"name": "Alice", "age": 30}; Root element name: person; Include XML declaration: yes; Indentation spaces: 2<?xml version="1.0" encoding="UTF-8"?> / <person> / <name>Alice</name> / <age>30</age> / </person>
Array of itemsJSON input: {"fruits": ["apple", "banana", "cherry"]}; Root element name: data; Include XML declaration: no; Indentation spaces: 2<data> / <fruits>apple</fruits> / <fruits>banana</fruits> / <fruits>cherry</fruits> / </data>
Nested with nullsJSON input: {"user": {"id": 1, "email": null, "roles": ["admin", "editor"]}}; Root element name: response; Include XML declaration: yes; Indentation spaces: 4<?xml version="1.0" encoding="UTF-8"?> / <response> / <user> / <id>1</id> / <email/> / <roles>admin</roles> / <roles>editor</roles> / </user> / </response>

FAQ

How are JSON arrays handled in the XML output?

Each item in a JSON array becomes a separate XML element using the parent key as the tag name. For example, {"color": ["red", "blue"]} becomes <color>red</color><color>blue</color>.

What happens to null values?

Null values are converted to self-closing empty XML elements. A key named middleName with a null value becomes <middleName/>.

Can I change the root element name?

Yes. Enter any valid XML element name in the Root element name field. If the name contains invalid characters, it will be sanitized automatically and reported in a note.

Is my data sent to a server?

No. The json to xml conversion runs entirely in your browser using JavaScript. Nothing is uploaded anywhere.

Does the converter preserve key order?

Yes. Keys appear in the XML output in the same order they are returned by JSON.parse, which follows the insertion order for string keys.

What if my JSON is invalid?

The tool displays the exact error message from JSON.parse, including the position where parsing failed, so you can fix the syntax quickly.

Are special characters escaped properly?

Yes. Characters like ampersands, angle brackets and quotes in string values are replaced with their standard XML entity equivalents.