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:
- Element to Property: Each XML element name becomes a key in a JSON object.
- 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). - 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.
- 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.
- Attribute and Namespace Omission: All XML attributes, processing instructions, and namespace identifiers are discarded during conversion.
- Empty Elements: Empty XML tags (e.g.,
<item/>or<item></item>) map directly tonullor 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
- Minimal Payload Size: By eliminating metadata
markers like
@prefixes, XML namespaces, and explicit attribute wrappers, data size is significantly reduced. - Idiomatic JSON: The output matches standard JSON design patterns, making it straightforward to consume in JavaScript and modern API environments.
- Low Complexity: Parsing logic remains simple because developers do not need to navigate nested metadata objects to retrieve values.
Limitations
- Lossy Data Structure: Because attributes and namespace contexts are ignored, the conversion cannot be reversed back to the original XML without data loss.
- Mixed Content Handling: XML elements containing both raw text and child elements (mixed content) are not supported well and typically have their text nodes dropped or overridden.