XML guides

Choose an XML to JSON mapping that keeps the information you need

·

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.

XML and JSON Converter

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.

W3C Extensible Markup Language 1.0

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.

W3C XPath and XQuery Functions and Operators — XML to JSON

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 with repeated elements
<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.

Sources

Put it into practice.

Related guides