Back to blog

JSON to XML Examples: Convert Objects, Arrays, and Attributes

JSON to XML conversion

JSON is common in browser and REST APIs, while XML is still used by SOAP services, RSS feeds, configuration files, and enterprise integrations. A JSON to XML converter turns an object-shaped payload into XML markup, but the result depends on consistent mapping rules.

Use the DevToolkit JSON to XML Converter to convert in either direction without uploading your payload.

Object to XML example

This JSON object:

{
  "user": {
    "id": 42,
    "name": "Amina"
  }
}

can become:

<user>
  <id>42</id>
  <name>Amina</name>
</user>

The outer object becomes the root element, and nested keys become child elements. A production integration should agree on the root name and schema with the receiving service.

Arrays become repeated elements

An array such as:

{
  "items": ["keyboard", "mouse"]
}

is commonly represented as:

<items>
  <item>keyboard</item>
  <item>mouse</item>
</items>

Some systems expect <items>keyboard</items> repeated instead. Confirm the target API's schema before relying on the output.

JSON attributes and text nodes

DevToolkit uses the @_ prefix to represent XML attributes:

{
  "product": {
    "@_id": "sku-7",
    "#text": "Keyboard"
  }
}

This can represent:

<product id="sku-7">Keyboard</product>

Attribute mapping is a convention, not a feature built into JSON itself. Document the convention whenever multiple services share payloads.

JavaScript conversion example

For application code, use a maintained XML library and validate the result against the target schema. A simplified mapping might look like this:

const order = {
  order: {
    id: "A-100",
    total: 29.99
  }
};

// The exact XML output depends on the library's root and array rules.

The online converter is useful for inspecting a representative payload before implementing a permanent integration.

JSON versus XML limitations

Conversion can change ordering, numeric types, namespaces, comments, CDATA, and mixed text. JSON has no native attribute or namespace model, and XML can distinguish values that JSON treats as the same key-value structure.

Read JSON versus XML, then test the result with the JSON Validator or JSON Formatter.

Summary

JSON to XML conversion is a mapping problem, not a perfect one-to-one translation. Define root, array, attribute, and text-node rules, validate the generated document, and test it against the receiving service.