Choose an XML to JSON mapping that keeps the information you need
Admin ·
XML can interleave child elements and words. JSON can distinguish a string from a number even when they look alike. The two mappings preserve different information.
Keep the words around an inline element
Enter <p>Tea <b>set</b> today</p> and convert XML to JSON with Ordered XML nodes. The root is p. Its children are the text Tea with a trailing space, a b element holding set, and the final text with a leading space. Copy the result into JSON to XML using the same mapping to restore the sequence.
Keep a number distinct from an identifier
Choose JSON to XML and Typed JSON values. Convert {"id":"007","count":9007199254740993,"items":[true,null]}. The id becomes a string element. The count becomes a number element with every digit intact, and the array retains a boolean and a null. Feed that XML into the reverse direction with the typed mapping to recover the values.
Round-trip repeated warnings and trailing text
Choose the ordered XML representation. The notice contains two elements with the same name and the word End outside both. Convert to JSON and inspect their positions in children.
<notice><warn>A</warn><warn>B</warn>End</notice>- Convert that JSON back to XML and read the content from left to right. Both warnings should survive in order and End should follow the second one. An object keyed only by warn cannot express both siblings without another representation.
Common questions
Why is the xmlns attribute present on the typed root?
It identifies the XPath functions vocabulary. A tag named number in an unrelated XML vocabulary can have different semantics and belongs in the ordered mapping.
What changes when I compact JSON?
Whitespace used for indentation disappears. Whitespace inside a JSON string remains part of that string. The output byte count measures the complete UTF-8 result.