By JSONHint · Updated
There is no single universal XML-to-JSON representation. JSONHint uses a particular mapping so the result can be inspected as an object. Confirm that mapping against the consumer's contract before using converted data.
Attributes, text and repeated siblings
<order id="42"><item>pen</item><item>paper</item></order>The converter produces:
{"order":{"@attributes":{"id":"42"},"item":["pen","paper"]}}Attributes are placed in @attributes. Repeated child names become an array. XML text leaves remain strings, so the id attribute is not automatically a number. With only one item, the result is a string rather than an array. Normalize that shape explicitly if the downstream API always expects an array.
Empty elements and text with attributes
<item/> becomes an empty object, not null or an empty array. <item unit="box">2</item> includes an attributes object and a #text property with the string value 2. The converter trims surrounding text whitespace.
Information you can lose
Comments and CDATA nodes are not preserved by this mapping. Mixed content is particularly risky: a paragraph such as <p>Hello <b>world</b>!</p> contains text before and after a child element. A property map cannot preserve that document order, and this converter can discard part of the text. Use an XML-aware document representation when order and markup are important.
Namespace-qualified names do not become a namespace-aware JSON contract automatically. Attribute ordering, XML declarations and other lexical details also cannot be reconstructed reliably from this output. Do not promise a lossless round trip from XML to JSON and back.
A safer migration workflow
- Validate that the source is well formed using the XML checker.
- Choose examples with zero, one and multiple repeated elements.
- Include attributes, empty elements, Unicode and mixed content in the fixtures.
- Convert and compare every output field with the target contract.
- Write explicit normalization code for arrays, types and null values.
Use the XML specification for document rules and RFC 8259 for JSON syntax. The conversion convention is an application decision, not a rule those specifications impose.
Found an error? Send a reproducible example or a source correction.