XML and JSON
Serialisation
Serialisation (also called marshalling or pickling) is the process of converting a data structure into a linear sequence of bytes for transfer over a network or for storage in a file.
- JSON was originally designed for serialising data — lightweight, simple, and easy to parse.
- XML was designed for both serialisation and marking up human-readable documents so different parts could be located or processed in different ways.
- CSV is also commonly used for data exchange between databases or applications.
- NoSQL systems may use XML or JSON as the primary (native) form of a stored document.
Abstract syntaxes
Both XML and JSON can be described by abstract tree types:
type xml_t = ELEMENT of string * (string * string) ulist * xml_t list
| LEAF of string
type json_t = LEAF_S of string
| LEAF_N of integer
| ARRAY of json_t list
| OBJECT of (string * json_t) ulist
| NULL
Both contain tree-structured text with named nodes. They are broadly similar in their representational power, though XML has attributes (the (string * string) ulist on each element) whereas JSON does not distinguish attributes from child nodes.
The XML structure spectrum
XML documents can lie anywhere on a spectrum of structural regularity:
| Structure | Description |
|---|---|
| All data in one large element | Unstructured blob; XML provides no benefit beyond a wrapper |
| Semi-structured | Some elements contain a lot of text, others contain atomic values |
| Every atomic value in its own element | Fully marked up; unrealistic in practice because of verbosity |
The schema rigorousness spectrum
| Rigorousness | Description |
|---|---|
| Precise schema with URL | Document points to a formal schema (DTD, XML Schema) at a known URI |
| Relaxed schema | Schema exists but is loosely enforced |
| Extra attributes allowed | Schema defines a minimum; additional attributes may appear beyond it |
| No schema at all | Document is purely self-describing; structure is whatever the programmer believes it to be |
JSON as the dominant format
JSON has largely displaced XML for new applications. Reasons include:
- Lighter syntax: fewer angle brackets and closing tags.
- Direct mapping to native types in JavaScript, Python, and similar languages.
- Faster parsing.
- Simpler mental model: objects, arrays, strings, numbers, booleans, null.
XML retains advantages where markup of document text is needed (mixing tags and prose), where namespaces are important, or where an ecosystem of standardised tools (XSLT, XPath, XQuery) already exists.
Comparison
| Feature | JSON | XML |
|---|---|---|
| Data model | Objects, arrays, scalars | Elements with attributes and children |
| Schema language | JSON Schema (optional) | DTD, XML Schema, Relax NG |
| Query language | JSONPath, jq | XPath, XQuery |
| Mixed content | Not supported | Supported (text interspersed with tags) |
| Namespaces | Not built in | Built in |
| Readability | High for data | Good for marked-up documents |