How BadgerFish Translates XML to JSON
The BadgerFish convention is a specialized mapping protocol designed to convert complex XML documents into JSON format without losing structural metadata such as attributes and namespaces. While basic XML-to-JSON conversions often discard attributes or struggle with mixed content, BadgerFish establishes specific syntactic rules to ensure a lossless, bidirectional translation between the two data formats.
1. Element Values
In BadgerFish, the text content of an XML element is mapped to a key
named "$" within a JSON object. If an element contains only
text and no attributes or child elements, it is still represented as an
object with this text property to maintain a uniform structure.
- XML:
<title>Data Systems</title>- BadgerFish JSON:
{
"title": {
"$": "Data Systems"
}
}2. XML Attributes
XML attributes are mapped to keys prefixed with the "@"
symbol. This syntax cleanly separates element properties from child
elements and text content.
- XML:
<user id="101" active="true">Alice</user>- BadgerFish JSON:
{
"user": {
"@id": "101",
"@active": "true",
"$": "Alice"
}
}3. XML Namespaces
Namespaces are handled through a dedicated "@xmlns"
object. * A default namespace is represented using "$". *
Prefixed namespaces use the prefix name as the key within the
"@xmlns" object. * Elements utilizing a namespace prefix
retain that prefix in their key name.
- XML:
<book xmlns="http://example.com/books" xmlns:meta="http://example.com/meta">
<meta:isbn>123456</meta:isbn>
</book>- BadgerFish JSON:
{
"book": {
"@xmlns": {
"$": "http://example.com/books",
"meta": "http://example.com/meta"
},
"meta:isbn": {
"$": "123456"
}
}
}4. Nested Elements and Arrays
Child elements are nested as keys within their parent’s JSON object. When an element contains multiple sibling elements with the same tag name, BadgerFish groups them into a standard JSON array instead of overwriting identical keys.
- XML:
<catalog>
<item id="1">Book</item>
<item id="2">Magazine</item>
</catalog>- BadgerFish JSON:
{
"catalog": {
"item": [
{
"@id": "1",
"$": "Book"
},
{
"@id": "2",
"$": "Magazine"
}
]
}
}Summary of Core Rules
"$": Holds the textual value of an element."@attribute": Holds attribute values, prefixed with@."@xmlns": Stores namespace URIs and prefixes.- Arrays
[...]: Used automatically whenever multiple child tags share the same name within a single parent.