Oct
9

JSON to XML: When and How to Convert Between Them

10/09/2026 02:28 PM 작성자 Seovaro Team 의


JSON to XML: When and How to Convert Between Them

JSON won the API wars years ago, but XML never actually left. It still runs SOAP services, enterprise configurations, RSS feeds, Office documents, and countless legacy integrations that are not going anywhere. Sooner or later, most developers face the same chore: data arrives as JSON and needs to leave as XML, or the reverse. Understanding how the two formats map to each other — and where the mapping breaks — saves hours of debugging. When you need the conversion done now, our free JSON to XML converter handles it instantly in the browser.

This guide covers how JSON and XML differ structurally, the situations where conversion is actually necessary, the mapping rules that turn objects into elements and arrays into repeated nodes, and the pitfalls that catch people out: attributes, namespaces, and the mixed-content problem.

JSON vs XML: The Structural Differences

Both formats describe hierarchical data, but they think about it differently:

  • JSON has a small set of types: objects, arrays, strings, numbers, booleans, and null. It maps directly onto the data structures of modern programming languages, which is why APIs love it.
  • XML has one core concept — the element — plus attributes, namespaces, and mixed content (text interleaved with child elements). It is more expressive and more verbose.

The same data in both formats:

// JSON
{
  "book": {
    "title": "Dune",
    "author": "Frank Herbert",
    "year": 1965,
    "tags": ["sci-fi", "classic"]
  }
}
<!-- XML -->
<book>
  <title>Dune</title>
  <author>Frank Herbert</author>
  <year>1965</year>
  <tags>sci-fi</tags>
  <tags>classic</tags>
</book>

Notice what happened to the array: JSON's ["sci-fi", "classic"] became two repeated <tags> elements, because XML has no native array type. That single difference drives most conversion complexity.

When Conversion Is Actually Necessary

You do not convert formats for fun. The real-world triggers:

  • Legacy SOAP APIs. Enterprise systems in banking, logistics, and government still speak SOAP, which is XML-only. Your modern JSON microservice needs a translation layer at the boundary.
  • Configuration files. Tools like Maven (pom.xml), Android layouts, and SVG-adjacent pipelines expect XML. Build scripts often generate them from JSON sources.
  • RSS/Atom feeds. Feed readers consume XML. If your content lives in a JSON-based CMS, feed generation is a JSON-to-XML transform.
  • Document formats. DOCX, XLSX, and other Office formats are XML under the hood. Generating them programmatically means producing XML.
  • Regulatory and EDI formats. Invoicing standards, customs declarations, and healthcare data exchange frequently mandate XML schemas.
  • XSLT pipelines. If the downstream transform is XSLT, the input must be XML regardless of where the data originated.

Mapping Rules: Objects, Arrays, and Primitives

A reliable converter follows predictable rules. Knowing them lets you predict the output and design JSON that converts cleanly:

  • Objects → elements. Each key becomes a child element: {"name": "Ada"} → <name>Ada</name>.
  • Arrays → repeated elements. Each item becomes a sibling element with the same tag name, as shown in the book example above.
  • Strings and numbers → text content. Values become the text inside their element. Numbers stay as text — XML has no number type, so type information is lost unless the schema restores it.
  • Booleans → text. true becomes <active>true</active>. Some converters use 1/0; check your tool's convention.
  • Null → empty element or omission. {"middle": null} typically becomes <middle/>. Some schemas prefer the element omitted entirely — confirm what your consumer expects.
  • Nested objects → nested elements. The hierarchy maps one-to-one, which is the easy part.

The Attribute Problem

Here is where naive conversion breaks down. JSON has no concept of attributes, so a converter must decide: does {"id": 7} become <id>7</id> (element) or <item id="7"/> (attribute)?

Common conventions:

  • Prefix convention. Keys starting with @ or - become attributes: {"@id": 7} → <item id="7"/>. This is the most widely used approach.
  • #text convention. When an element needs both attributes and text content, the text goes in a special key: {"@lang": "en", "#text": "Hello"} → <greeting lang="en">Hello</greeting>.
  • Schema-driven. Enterprise converters use an XSD schema to decide what becomes an attribute. Most accurate, most setup work.

If your target system validates against a schema, find out its attribute rules before converting. A converter's default guess is right maybe 80% of the time, and the remaining 20% fails validation in confusing ways.

Common Pitfalls

  • Root element requirement. XML documents must have exactly one root element. JSON like {"a": 1, "b": 2} needs a wrapper: <root><a>1</a><b>2</b></root>. Converters add one automatically — make sure its name matches what the consumer expects.
  • Namespaces. Enterprise XML uses namespaces (<soap:Envelope>). Plain JSON-to-XML output has none, so SOAP envelopes and similar structures need post-processing or a namespace-aware converter.
  • Special characters. <, >, and & in JSON strings must be escaped as &lt;, &gt;, &amp; in XML. Good converters do this automatically; hand-rolled string concatenation does not.
  • Mixed content. XML like <p>Hello <b>world</b></p> (text mixed with elements) has no natural JSON equivalent. If your data includes rich text, conversion gets lossy — consider whether the rich text should travel as an escaped string instead.
  • Order sensitivity. JSON object key order is technically insignificant; XML element order can be significant to a validating parser. If the schema demands <first> before <last>, your converter must preserve insertion order.
  • Type loss. Everything becomes text in XML. The number 1965, the boolean true, and null all arrive as strings. The consumer's schema or parsing code must restore the types.

Going the Other Direction: XML to JSON

XML-to-JSON has its own quirks. Repeated elements collapse into arrays (good), but a single <tags> element becomes a plain string while two become an array — an inconsistency that breaks strongly-typed consumers. This is the classic "array of one" problem. Robust converters offer an option to always create arrays for specified elements. Attributes need the reverse of the prefix convention, and namespaces either get stripped or prefixed into key names.

If you control both ends, the cleanest approach is to define the mapping explicitly rather than relying on any converter's defaults. Conventions beat cleverness in format translation.

Final Thoughts

JSON-to-XML conversion is straightforward for simple data and fiddly at the edges — attributes, namespaces, mixed content, and type fidelity are where the dragons live. For one-off conversions and quick checks, an automated tool is the fastest path: paste your JSON into our JSON to XML converter and inspect the output before wiring it into a pipeline. For production integrations, pin down the attribute conventions and schema requirements first, then automate with a library in your stack.

FAQ

How do JSON arrays convert to XML?

Arrays become repeated sibling elements with the same tag name. {"tags": ["a", "b"]} becomes <tags>a</tags><tags>b</tags>, because XML has no native array type.

How are XML attributes represented in JSON?

By convention, most converters use a prefix: keys starting with @ (or -) become attributes. {"@id": 7} converts to <item id="7"/>. Text content alongside attributes typically uses a #text key.

Why does my converted XML fail schema validation?

The usual suspects: wrong root element name, attributes emitted as elements (or vice versa), missing namespaces, element order violations, or unescaped special characters. Validate early against the actual XSD rather than eyeballing the output.

Is JSON or XML better for APIs?

JSON, for almost all new APIs — it is more compact, maps directly to language data structures, and is easier to read. XML remains the right choice where schemas, namespaces, document validation, or legacy system compatibility are required.