XML to JSON mapping guide

What happens to empty XML elements in JSON?

Answer for ParseBloom: An empty element becomes an empty string, while an element that was never present has no JSON property. Whitespace-only text also becomes an empty string. Repeated empty siblings form an array of empty strings. Check for property presence before treating an empty value as missing.

Reproducible fixtures for the ParseBloom XML to JSON converter, 27 October 2026. Mapping choices vary between converters.

The distinction at a glance

Present empty elements versus absent elementsA present empty child maps to a JSON key with an empty string. A missing child has no corresponding JSON key. Two empty children map to an array with two empty strings.<r><e/></r>{"r":{"e":""}}<r/>{"r":""}<r><e/><e/></r>{"r":{"e":["",""]}}

In the middle row, r itself exists and is empty. There is no child e. The illustration uses short names; the full copyable fixtures below use empty.

Six copyable cases

Paste each XML snippet into the converter and click Convert to JSON. The JSON below is compacted for comparison; the tool displays indented JSON. These are synthetic inputs, so no private data is involved.

1 · Self-closing child
<r><empty/></r>
JSON
{"r":{"empty":""}}

The child is present. Its value is a string of length zero.

2 · Explicit closing tag
<r><empty></empty></r>
JSON
{"r":{"empty":""}}

The two XML spellings produce the same JSON. This output cannot tell which spelling was used.

3 · Whitespace-only child
<r><empty>   </empty></r>
JSON
{"r":{"empty":""}}

This mapper trims text. Spaces that were meaningful to the producer disappear.

4 · Child absent
<r><present>A</present></r>
JSON
{"r":{"present":"A"}}

There is no empty property. An absent child is different from a present child with an empty value.

5 · Repeated empty children
<r><empty/><empty/></r>
JSON
{"r":{"empty":["",""]}}

The repeated name triggers an array, even though both values are empty strings.

6 · Empty child with an attribute
<r><empty id=""/></r>
JSON
{"r":{"empty":{"_attributes":{"id":""}}}}

An attribute changes the child's shape to an object. Its own empty value still survives.

Test presence before testing content

A common shortcut, if (!row.empty), treats an absent key, an empty string, and other falsy values alike. Use an own-property check when your application needs to distinguish “the XML supplier sent an empty field” from “the supplier did not send this field.” Then inspect the value's type, because repeated elements and attributed elements do not have the single-string shape.

const row = converted.r;
const sent = Object.hasOwn(row, 'empty');
if (!sent) {
  // No <empty> child was present.
} else if (row.empty === '') {
  // A single empty or whitespace-only child was present.
} else if (Array.isArray(row.empty)) {
  // Repeated children: inspect each array entry.
} else if (typeof row.empty === 'object' && row.empty !== null) {
  // Check _attributes and any child values.
}

This code checks the JSON output of this particular mapper. It does not infer whether the XML used <empty/> or a separate closing tag, and it cannot recover whitespace removed by trimming.

Do not assume every converter uses an empty string

The JSON Checker XML-to-JSON page documents a different choice: empty elements become null. The CodeShack converter exposes mapping options such as attribute handling and type parsing. The JSON Formatter converter page provides another interface. Compare actual output before wiring a consumer to any converter's JSON shape.

A public Json.NET question about an empty XML element illustrates why this choice matters. The examples here answer it only for ParseBloom's mapping; they are not a claim about Json.NET.

For the related one-versus-many shape, see the repeated-elements guide. For attr-only objects and text-like IDs, see the attributes guide.

Try the XML to JSON converter