Skip to content
Part IA Michaelmas Term

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:

StructureDescription
All data in one large elementUnstructured blob; XML provides no benefit beyond a wrapper
Semi-structuredSome elements contain a lot of text, others contain atomic values
Every atomic value in its own elementFully marked up; unrealistic in practice because of verbosity

The schema rigorousness spectrum

RigorousnessDescription
Precise schema with URLDocument points to a formal schema (DTD, XML Schema) at a known URI
Relaxed schemaSchema exists but is loosely enforced
Extra attributes allowedSchema defines a minimum; additional attributes may appear beyond it
No schema at allDocument 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

FeatureJSONXML
Data modelObjects, arrays, scalarsElements with attributes and children
Schema languageJSON Schema (optional)DTD, XML Schema, Relax NG
Query languageJSONPath, jqXPath, XQuery
Mixed contentNot supportedSupported (text interspersed with tags)
NamespacesNot built inBuilt in
ReadabilityHigh for dataGood for marked-up documents