Skip to content
ToolkitHub

JSON to XML

Convert JSON to XML

Guide

Mapping JSON data onto XML

By Logappradeep · ServiceNow developer

XML is still the required format for SOAP web services, many banking and government integrations, RSS feeds, sitemaps, Android resources, and older enterprise systems. When your data lives in JSON, a converter saves you from hand-writing angle brackets. The catch is that the two formats have different data models, so every converter has to choose a set of mapping rules.

How this converter maps JSON to XML

  • Object keys become element names, and their values become the element content.
  • Arrays become repeated elements that share the parent key's name.
  • Keys starting with @ become attributes on the parent element, so "@id": "7" produces id="7".
  • A #text key sets an element's text content directly, which lets an element have both attributes and text.
  • null values become empty self-closing elements.
  • Special characters such as <, &, and quotes in values are escaped automatically.
  • An XML declaration (<?xml version="1.0" encoding="UTF-8"?>) is added at the top.

For example, this JSON:

{"user": {"@id": "7", "name": "Ada", "tag": ["math", "code"]}}

produces:

<?xml version="1.0" encoding="UTF-8"?>
<user id="7">
  <name>Ada</name>
  <tag>math</tag>
  <tag>code</tag>
</user>

The root element

Well-formed XML needs exactly one root element. If your JSON object has a single top-level key, that key becomes the root, as with user above. If it has several top-level keys, they are wrapped in a <root> element, and a top-level array becomes <root> containing one <item> per entry. If the receiving system expects a specific root name, wrap your data in an object with that single key before converting.

Pitfalls to check before you ship the output

  • Not every JSON key is a legal element name. XML names cannot contain spaces and cannot start with a digit. The converter repairs them by replacing invalid characters with underscores and prefixing an underscore when needed, so "first name" becomes first_name and "2fa" becomes _2fa. Rename keys yourself if the consumer expects exact names.
  • Types disappear. XML text has no numbers or booleans, so 36 and true become plain text. The receiving system needs a schema or its own rules to restore types.
  • One-item arrays look like single values. An array with one entry produces one element, indistinguishable from a non-array field. That matters if the XML is later converted back to JSON.
  • Namespaces are not managed for you. You can write prefixed names and @xmlns attributes by hand, but the converter does not validate them.

When to reach for this tool

It is handy for building sample SOAP request bodies, preparing test fixtures for an XML-only API, generating simple feed or config snippets, and checking how a JSON payload would look to a legacy partner system. For production pipelines with strict schemas (XSD), a dedicated mapping layer in your code gives you more control.

The conversion runs live in your browser as you type; your JSON is not uploaded anywhere.

Frequently asked questions

How do I create XML attributes?

Prefix the key with @. For example, {"item": {"@id": "1", "#text": "Pen"}} produces <item id="1">Pen</item>.

Why is my output wrapped in a <root> element?

XML allows only one root element. When the JSON has several top-level keys, or is an array, the converter wraps everything in <root> to keep the document well-formed.

Why did my key names change?

Keys containing spaces or other characters not allowed in XML names, or starting with a digit, are repaired with underscores so the output stays valid.

Are numbers and booleans preserved?

They are written as text, because XML has no native number or boolean types. The consumer decides how to interpret them.