JSON Arrays to XML Repeated Elements

Learn how JSON arrays map to repeated XML elements. Covers simple arrays, arrays of objects, nested arrays, and different naming conventions for array items.

Structure

Detailed Explanation

Arrays are the most challenging part of JSON-to-XML conversion because XML has no native array type. Instead, arrays are represented by repeating sibling elements with the same tag name.

A JSON array of primitives:

{
  "colors": ["red", "green", "blue"]
}

Common XML representations:

Option 1: Wrapper + repeated element

<colors>
  <color>red</color>
  <color>green</color>
  <color>blue</color>
</colors>

Option 2: Repeated element only (no wrapper)

<color>red</color>
<color>green</color>
<color>blue</color>

Arrays of objects:

{
  "users": [
    { "name": "Alice", "role": "admin" },
    { "name": "Bob", "role": "editor" }
  ]
}
<users>
  <user>
    <name>Alice</name>
    <role>admin</role>
  </user>
  <user>
    <name>Bob</name>
    <role>editor</role>
  </user>
</users>

Naming conventions for array items:

Most converters derive the singular form of the item element name from the plural wrapper:

  • users -> user
  • items -> item
  • entries -> entry

Some converters use a generic <item> tag or let you configure the child element name.

Nested arrays (arrays within arrays):

{ "matrix": [[1, 2], [3, 4]] }

This is particularly tricky in XML. One approach:

<matrix>
  <row>
    <item>1</item>
    <item>2</item>
  </row>
  <row>
    <item>3</item>
    <item>4</item>
  </row>
</matrix>

The reverse problem (XML to JSON) is equally challenging: when you see repeated sibling elements, should they become a JSON array? Most converters use a heuristic: if two or more siblings share the same tag name, they become an array. A single element becomes an object. This inconsistency is a common source of bugs.

Use Case

Converting a JSON API response containing a list of order line items into XML format for submission to a warehouse management system that processes XML-based purchase orders.

Try It — JSON ↔ XML Converter

Open full tool