Tools, FAQ, Tutorials:
Mapping JSON Values to XML Elements
What is the standard to map JSON values to XML elements?
✍: FYIcenter.com
There seems to be no industry standard on how to map JSON values to XML elements.
But there are some commonly used conventions:
1. Automatically adding a root element with "root" or "document" as the element tag. For example:
JSON:
{
"a": 1,
"b": 2
}
XML:
<root>
<a>1</a>
<b>2</b>
</root>
2. JSON String and Boolean values are converted to XML text content as is. For example:
JSON:
{
"a": "20.20",
"b": true
}
XML:
<root>
<a>20.20</a>
<b>true</b>
</root>
3. JSON Number values are normalized first and then converted to XML text contents. For example:
JSON:
{
"a": 20.0200,
"b": 0.314159e01
}
XML:
<root>
<a>20.02</a>
<b>3.14159</b>
</root>
4. JSON Null values are converted to empty XML text contents. For example:
JSON:
{
"a": null
}
XML:
<root>
<a/>
</root>
5. JSON Array values are converted to a sequence of XML sub elements with "element" at the tag. For example:
JSON:
[
2,
4,
8
]
XML:
<root>
<element>2</element>
<element>4</element>
<element>8</element>
</root>
6. JSON Object values are converted to a sequence of XML sub elements with their property names as element tags. Any illegal characters in XML tag names are placed with "_". XML tag names will be prefixed with "_" if they start with numeric characters. For example:
JSON:
{
"a": null,
"x+y": "X plus Y",
"x-y": "X minus Y",
"007": "Double O Seven",
"fyi.pi": 0.314159e01,
"fyi:pi": 0.314159e01,
"fyi_pi": "0.314159e01",
"fyi>pi": "31.4159e-1",
"error msg": "Something wrong!"
}
XML:
<root>
<a/>
<x_y>X plus Y</x_y>
<x-y>X minus Y</x-y>
<_007>Double O Seven</_007>
<fyi.pi>3.14159</fyi.pi>
<fyi_pi>3.14159</fyi_pi>
<fyi_pi>0.314159e01</fyi_pi>
<fyi_pi>31.4159e-1</fyi_pi>
<error_msg>Something wrong!</error_msg>
</root>
2023-07-11, ∼1940🔥, 0💬
Popular Posts:
How to use .NET CLR Types in Azure API Policy? By default, Azure imports many basic .NET CLR (Common...
How to use the "Ctrl-p Ctrl-q" sequence to detach console from the TTY terminal of container's runni...
How To Truncate an Array in PHP? If you want to remove a chunk of values from an array, you can use ...
What is EPUB 3.0 Metadata "dcterms:modified" property? EPUB 3.0 Metadata "dcterms:modified" is a req...
How To Protect Special Characters in Query String in PHP? If you want to include special characters ...