XML technologies / JSON for XML professionals / Syntax and basic forms
The JSON grammar itself is small. That makes it easy to get started, but should not suggest that any arbitrary JavaScript-looking object is a valid JSON file. The following section presents the basic forms individually and then combines them in a single example document.
Objects and arrays
An object consists of names and values. Names are strings enclosed in double quotation marks. A colon separates a name from its value; commas separate members. An array contains an ordered sequence of values. Objects use curly braces, and arrays use square brackets. [Q01]
{
"title": "Schematron",
"authorCount": 3
}
["XML", "JSON", "Publishing"]An array can also contain objects. This is particularly important for repeatable information in publishing:
{
"authors": [
{"id": "person-001", "name": "Manuel Montero Pineda"},
{"id": "person-002", "name": "Marko Hedler"},
{"id": "person-003", "name": "Nico Kutscherauer"}
]
}Here, author order is expressed by the array, not by the order of object members. Do not start with fields such as author1, author2 and author3. Such a model moves repetition into field names and makes generic processing harder. It is equally unhelpful to return an object for one author and an array only for several authors. A stable interface contract uses an array in both cases.
The six kinds of values
JSON supports strings, numbers, the two Boolean values, null, objects and arrays. These are usually counted as six type categories because true and false together form the Boolean type. The following complete document shows all the categories and several empty values. [Q04]
File: data/syntax-values.json
{
"text": "Publishing",
"count": 3,
"ratio": 0.75,
"active": true,
"archived": false,
"subtitle": null,
"emptyText": "",
"emptyArray": [],
"emptyObject": {},
"ordered": [
"first",
"second"
]
}JSON notation | Interpretation in the example | Common misconception |
|---|---|---|
"3" | String consisting of one digit | This is not automatically the number 3. |
3 | Number | JSON itself does not distinguish a separate primitive integer type here. |
true | Boolean value | "true" would be a string. |
null | Explicit null value | The member is still present. |
[] | Array with no members | It is neither null nor a missing field. |
{} | Object with no members | It is neither an empty XML element nor an empty list. |
"" | Empty string | It must be distinguished from information that is unknown or has not been supplied. |
These distinctions become relevant later in schema validation and transformations. In a publishing model, for example, an empty list keywords: [] can mean that no keywords have been assigned yet. Whether a missing field instead means “not requested” is a decision of the interface contract, not a built-in JSON rule.
Even a single value such as 42, "ready" or null is a complete JSON text under RFC 8259. A root object is common in practice, but is not required by general JSON syntax. [Q04]
Strings and escaping
In XML, the first concern is & and <. In JSON, the initial concerns are double quotation marks, backslashes and control characters. The actual example file is:
File: data/escaping.json
{
"quote": "The editor said \"Ready\".",
"path": "C:\\publishing\\book.json",
"lines": "First line\nSecond line",
"ampersand": "Research & Development",
"lessThan": "x < y",
"unicode": "Café"
}In the value lines the JSON source contains the escape sequence \n. After parsing, the string contains a line break. An unescaped line break inside a JSON string is different and is not permitted. \" represents a quotation mark in the value, and \\ a backslash. \u00e9 and the corresponding directly written Unicode character can express the same string value. [Q01]
In a standalone JSON file, ampersands and less-than signs do not require XML entities. However, when JSON is embedded in HTML or its content is output as HTML, an additional context comes into play. JSON escaping is not HTML escaping. This distinction is one of the most important security rules for publishing applications.
Errors, extensions and interoperability rules
The following three forms are deliberately incorrect. The companion package uses them as negative parser tests.
{"title": "Schematron",}
{'title': 'Schematron'}
{"title": "Schematron" // comment
}The first example contains a trailing comma, the second uses single rather than double quotation marks, and the third contains a comment. Extended configuration formats may allow these forms, but that does not make them standard JSON. For an API, the agreed media type must match the actual format.
Duplicate member names are a different case:
{"title": "First title", "title": "Second title"}The distinction between grammar and interoperability matters here. RFC 8259 recommends unique names using SHOULD, rather than categorically excluding duplicates through the grammar. Parsers may respond differently. The package’s input validation therefore explicitly rejects duplicates. They are not misleadingly classified as the same kind of error as a missing quotation mark. [Q04]
A clear rule is useful for a project’s publishing profile: no duplicate names, no special numeric values such as NaN or Infinity, and no hidden syntax extensions. The parser must be configured accordingly. A successful load() alone does not prove that this stricter input rule was respected.