By JSONHint · Updated
Choosing a format starts with the contract between the producer and consumer. A small API object and a document with mixed text do not have the same requirements. This comparison uses one order so you can inspect both representations and reproduce the byte counts.
The same order, two representations
{"order":{"id":42,"paid":false,"items":["pen","paper"]}}<order><id>42</id><paid>false</paid><items><item>pen</item><item>paper</item></items></order>The JSON uses a numeric id, a Boolean paid flag and an ordered array. XML element text has no inherent Boolean or number type without an agreed interpretation or schema. Both documents can describe the order, but a converter must decide how those values map.
Measure the payload rather than guessing
| Fixture | UTF-8 bytes | gzip bytes, level 9 |
|---|---|---|
| JSON | 56 | 71 |
| XML | 93 | 82 |
Download order.json, order.xml and the measurement script into one directory. Run node measure-payloads.mjs. The files have no final newline. Different gzip implementations or settings can change compressed size.
These are byte counts for one tiny fixture, not a parsing-speed benchmark. gzip includes framing overhead, which matters for short inputs. Repeating element names compress differently from a long text value. Test representative production payloads before claiming a format saves bandwidth or CPU time.
Where the mapping becomes a design decision
- Arrays: JSON explicitly represents an array, including an empty array. XML uses repeated elements by convention; a single element does not say whether more were allowed.
- Attributes: XML can put an identifier in an attribute. JSON has no attribute syntax, so a mapping must reserve a property or flatten it.
- Null and empty: JSON distinguishes null, an empty string and a missing property. An empty XML element does not establish which one was intended.
- Mixed content: XML can preserve text around child elements, as in an annotated paragraph. An ordinary object mapping can lose that order.
A practical choice
Use the format your API or document contract requires. JSON is convenient when the structure is naturally objects, arrays and scalar values. XML is useful when document structure, namespaces or an existing XML vocabulary matters. Neither choice removes the need to validate input or define the contract.
Try the JSON formatter, inspect the XML-to-JSON converter, then compare the generated structure with the original. See RFC 8259 and the XML specification for the underlying syntax.
Found an error? Send a reproducible example or a source correction.