XML technologies / JSON for XML professionals / Data model compared with XML
The biggest misconceptions arise not from braces and quotation marks, but from transferring the data model. Automatically converting XML into nested objects does not yet determine how to handle attributes, text nodes, namespaces or repeated elements. The following book record makes these modeling decisions explicit.
The same business object in two representations
The JSON version contains metadata, a price, authors, keywords and an explicit null value:
File: data/book.json
{
"id": "book-001",
"title": "Schematron",
"language": "de",
"published": "2011-07-27",
"available": true,
"price": {
"amount": "29.90",
"currency": "EUR"
},
"authors": [
{
"id": "person-001",
"name": "Manuel Montero Pineda"
},
{
"id": "person-002",
"name": "Marko Hedler"
},
{
"id": "person-003",
"name": "Nico Kutscherauer"
}
],
"keywords": [
"XML",
"Schematron",
"Publishing"
],
"subtitle": "Effiziente Business Rules für XML-Dokumente",
"publisher": "dpunkt.verlag",
"isbn": "9783898647212"
}The XML version puts some information in attributes, some in elements, and repeated information in containers:
File: data/book.xml
<book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" id="book-001" available="true">
<title>Schematron</title>
<language>de</language>
<published>2011-07-27</published>
<price currency="EUR">29.90</price>
<authors>
<author id="person-001">
<name>Manuel Montero Pineda</name>
</author>
<author id="person-002">
<name>Marko Hedler</name>
</author>
<author id="person-003">
<name>Nico Kutscherauer</name>
</author>
</authors>
<keywords>
<keyword>XML</keyword>
<keyword>Schematron</keyword>
<keyword>Publishing</keyword>
</keywords>
<subtitle>Effiziente Business Rules für XML-Dokumente</subtitle>
<publisher>dpunkt.verlag</publisher>
<isbn>9783898647212</isbn>
</book>These two forms were deliberately designed to correspond. This is not a universal XML-to-JSON mapping. For example, available is modeled as an attribute in XML and as a Boolean object member in JSON. In XML, the price is element content with a currency attribute; in JSON, it is a separate object. Either approach can make sense for this purpose.
It is important to distinguish between the stored notation and the interpreted type. The XML attribute available="true" initially consists of a lexical representation. Schema validation or explicit conversion can turn it into a Boolean value. A JSON parser recognizes true as a Boolean value from its syntax alone. This does not mean that XML lacks data types; they come from a different layer.
Attributes and namespaces are not JSON properties
JSON has no built-in distinction between attributes and elements. Fields such as @id, _attributes or #text can be useful as part of a converter contract, but have no automatic special meaning in ordinary JSON. Two converters using different conventions can produce very different JSON documents from the same XML.
<book xmlns:dc="http://purl.org/dc/elements/1.1/">
<dc:title>Schematron</dc:title>
</book>{
"dc:title": "Schematron"
}
The second example does not automatically associate dc with an IRI. Here, the colon is simply part of the field name. In XML, the namespace name and local name together identify an expanded name; the prefix is merely its concrete notation. A JSON mapping that stores only prefixes or only local names can therefore lose important distinctions. [Q09]
A JSON-LD context can map terms to IRIs. However, this does not retroactively activate XML namespaces; it is a model of its own. For arbitrary XML, the first decision is therefore whether namespaces must be preserved for business reasons or whether an explicit projection onto another vocabulary is intended.
Order and repetition
In XML, child-node order is part of the document model. For attributes, however, an application should not rely on their lexical order. Mixed content makes this immediately apparent: the position of an em element within a paragraph changes which text is emphasized. [Q10]
In the JSON model developed here, such ordering is expressed exclusively through arrays. The following form remains stable even when only one person is involved:
{"authors": [{"id": "person-001", "name": "Manuel Montero Pineda"}]}An XML converter that emits an object for exactly one author element and an array for multiple author elements produces a data-dependent shape. Calling code then has to distinguish between several structural variants on every access. This is not an inevitable JSON problem, but an unsuitable interface contract.
Empty containers pose another risk. From <authors/> a generic converter may produce an empty string, null, an empty object or an empty array. Only the business model determines which form is needed. In the book model, authors is always an array; validation requires at least one entry.
Missing, null, empty and false
{}
{"subtitle": null}{"subtitle": ""}These three forms are distinct. There are also false, 0 and empty arrays. Using truth values indiscriminately in a mapping risks losing information: whether a member exists is not the same question as whether its value is considered true by a programming language.
In XML, a missing element, an empty element and a schema-backed xsi:nil="true" are likewise different modeling choices. A null value should therefore not simply be mapped to <subtitle/> if the target model must also express an empty string. The actual book file contains the real subtitle. For the null-value comparison, data/book-null-subtitle.json and data/book-null-subtitle.xml contain explicitly modified exercise copies with null or xsi:nil; they do not claim that the Schematron book has no subtitle. An alternative API could deliberately omit optional information. What matters is the documented meaning, not a supposedly universal equivalence.
Numbers, dates and exact monetary amounts
A JSON number does not automatically have the computational precision of the receiving program. This becomes apparent with long identifiers and decimal fractions. The example program reads an integer beyond JavaScript’s safe integer range and compares it with the same sequence of characters stored as a string:
File: examples/js/precision.mjs
const parsed = JSON.parse('{"identifier":9007199254740993}');
const correct = JSON.parse('{"identifier":"9007199254740993"}');
console.log(JSON.stringify({
parsedAsNumber: parsed.identifier,
safeInteger: Number.isSafeInteger(parsed.identifier),
preservedAsString: correct.identifier,
floatSum: 0.1 + 0.2,
omittedUndefined: JSON.stringify({ subtitle: undefined }),
retainedNull: JSON.stringify({ subtitle: null })
}, null, 2));
Actual output produced by the included Node.js test:
File: results/core/number-precision.json
{
"parsedAsNumber": 9007199254740992,
"safeInteger": false,
"preservedAsString": "9007199254740993",
"floatSum": 0.30000000000000004,
"omittedUndefined": "{}",
"retainedNull": "{\"subtitle\":null}"
}The correct conclusion is not that JSON is fundamentally unable to carry large numbers. What matters is the numeric representation used by the parser and subsequent processing. For identifiers that are not used in calculations, a string is often the more suitable contract. For prices, the example publisher uses a decimal string with two fractional digits and an explicit currency. This is an EUR profile defined here, not a general rule for all currencies and financial data.
Dates are not a separate group of primitive types in JSON either. In the book model, 2011-07-27 is carried as a string; schema validation interprets its format. Python can selectively parse numeric JSON values as Decimal rather than first converting them into binary floating-point numbers. [Q11]