Converting XML to JSON with Parker Convention

The Parker convention is a streamlined, lossy transformation algorithm designed to convert XML documents into JSON objects without retaining extraneous metadata. This article explores how the Parker convention functions, its foundational conversion rules, an illustrative example, and its key advantages and limitations when handling data interchange.

Core Principles of the Parker Convention

Unlike conventions such as BadgerFish, which preserve XML attributes and namespaces using specialized prefix keys, the Parker convention prioritizes simplicity and minimal payload size. It assumes that XML attributes and namespace declarations are either redundant or unnecessary for the target application.

The conversion follows these strict mapping rules:

  1. Element to Property: Each XML element name becomes a key in a JSON object.
  2. Text Content to Primitive Values: If an XML element contains only text content and no child elements, the value is converted directly to a native primitive (string, number, boolean, or null).
  3. Child Elements to Nested Objects: If an XML element contains distinct child elements, it is transformed into a nested JSON object where each key represents a child element.
  4. Repeated Elements to Arrays: If an element contains multiple sibling elements sharing the same tag name, those elements are consolidated into a JSON array of values or objects.
  5. Attribute and Namespace Omission: All XML attributes, processing instructions, and namespace identifiers are discarded during conversion.
  6. Empty Elements: Empty XML tags (e.g., <item/> or <item></item>) map directly to null or empty strings depending on the parser implementation.

Example Transformation

Consider the following XML snippet containing product information with an attribute and repeated elements:

<product id="101">
    <name>Wireless Mouse</name>
    <price>29.99</price>
    <inStock>true</inStock>
    <tags>
        <tag>electronics</tag>
        <tag>accessories</tag>
    </tags>
</product>

When converted using the Parker convention, the resulting JSON is:

{
  "product": {
    "name": "Wireless Mouse",
    "price": 29.99,
    "inStock": true,
    "tags": {
      "tag": [
        "electronics",
        "accessories"
      ]
    }
  }
}

Notice that the id="101" attribute on the <product> element was dropped, and the sibling <tag> elements were cleanly collapsed into a single array.

Advantages

Limitations