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"producesid="7". - A
#textkey sets an element's text content directly, which lets an element have both attributes and text. nullvalues 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"becomesfirst_nameand"2fa"becomes_2fa. Rename keys yourself if the consumer expects exact names. - Types disappear. XML text has no numbers or booleans, so
36andtruebecome 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
@xmlnsattributes 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.