2.5 Conclusions
3.1.2 JavaScript Object Notation (JSON)
JavaScript Object Notation [Bra14], better known as JSON, is a data format born from the JavaScript Programming Language. However, JSON is a text-based format as well as language independent and is not tailored to JavaScript Although being a text-based data format, JSON was designed to be relatively lightweight and easy to parse, at least, compared to XML.
<schema
xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:pe="http://example.com/namespaces/pets"
targetNamespace="http://example.com/namespaces/pets"
elementFormDefault="qualified"> <element name="pet">
<complexType> <sequence>
<element name="name"type="string"/> <element name="age" type="integer"/> <element name="gender" type="string"/> </sequence>
<attribute name="species" type="string"/> </complexType>
</element> </schema>
Figure 3.8: XML Schema simple example.
{
"name":"Calcetines",
"age": 1,
"gender":"Female",
"owners": ["Maite","Jorge"] }
Figure 3.9: JSON simple structure example.
The JSON format structure is based on two constructs: name/value pairs and arrays of values. JSON can also represent four basic types: strings, numbers, booleans and null. In JSON, objects are unordered sequences of name/value pairs separated by a comma (i.e., ‘,’), where the name is always represented as a string and the value is either a string, number, boolean, null, array or another nested object. JSON objects are enclosed between curly braces (i.e., ‘{’ and ‘}’). A JSON array is an ordered sequence of zero or more values separated by a comma (i.e., ‘,’), where values are strings, numbers, booleans, null, or nested arrays and objects. JSON arrays are enclosed between square brackets (i.e., ‘[’ and ‘]’). A simple example of a JSON document is shown in Figure 3.9.
JSON structure components are described in more detail in the following list:
• Literals: JSON specifies three literals: “true”, “false” and “null”. The first two (“true” and “false”) are used for boolean values while the third one (“null”) is used to represent null
values.
• Numbers: JSON numbers are represented as a base 10 sequence of decimal digits. JSON numbers can be prefixed with a sign symbol (‘+’ or ‘-’) and may include a fractional part. The fractional part can be either represented with a dot (i.e., ‘.’) separated digits or with an exponent of ten prefixed by an ‘e’ or ‘E’. Notably, JSON does not support numbers that cannot be represented as sequences of digits (such as INF and NaN).
Section 3.1. Text-Based Data Formats 25
• Strings: a JSON string is a sequence of Unicode characters enclosed in quotation marks. A reverse solidus (i.e., ‘\’) is used to escape characters. Characters in the Basic Multilingual Plane (U+0000 through U+FFFF) may be optionally represented as a reverse solidus, followed by the letter ‘u’, followed by the four hexadecimal code point digits. For instance, the letter ‘A’ may be optionally encoded as “\u0041”. Extended characters that are not in the Basic Multilingual Plane, can be represented by combining characters within the Basic Multilingual Plane.
• Objects: objects are used to represent structured data composed by one or more fields. JSON objects are represented as a sequence of name/value pairs enclosed by curly braces. Names are represented as quoted strings and are separated by a colon (i.e., ‘:’) from the value. Consecutive value pairs are separated by a comma (i.e., ‘,’).
The box below shows an example of a JSON object with two subschemas. { "name1": value1, "name2": value2 }
• Arrays: array represent a sequence of values. JSON arrays are represented as a comma separated sequence of zero or more values enclosed between square brackets. The values of an array can be of different types.
The box below contains a JSON array with three subschemas. [ value1, value2, value3 ]
3.1.2.1 JSON Schema
A JSON Schema defines the structure of JSON data as well as provide information for validation, parsing and interaction. At the time of this writing Draft-04 version is still the more widely used JSON Schema version, compared to more recent ones (currently Draft-07 [WA18, WAL18]). Thus, this thesis focuses on JSON Schema Draft-04. However, the principles described in this thesis are easily extrapolated to more recent JSON Schema versions, such as Draft-07.
The JSON Schema Draft-4 specification is actually composed by three documents.
• JSON Schema core specification [GZC13] describes the core terminology, references to other JSON Schemas and vocabulary definition.
• JSON Schema Validation [ZC13] defines the vocabulary for validation assertions, link navigation and interaction constraints.
• JSON Hyper-Schema specification [LZC13] describe the hypertext structure and man- agement of JSON documents such as resource link relations and multimedia vocabulary.
{
"title":"root schema",
"sub": {
"title":"subschema"
} }
Figure 3.10: JSON Schema root schema and subschema example.
A JSON Schema is in itself a JSON document, but it is used to define the data model followed by other JSON documents, known as “instances”. Thus, a JSON Schema defines the structure and constraints over the same structural components used by JSON documents and listed in the previous section, Section 3.1.2: null, boolean, number, string, object and array.
A JSON Schema document always starts from the root schema, but it can contain any number of nested schemas, denoted subschemas. For instance, Figure 3.10 shows an example JSON Schema that is composed by a root schema (titled “root schema”) and one subschema (titled “subschema”).
The root schema and subschemas of a JSON document are either an object or a boolean. Schemas with boolean root elements are especial schemas that either always pass validation of instances (“true”) or always fail (“false”). If the schema is an object, it contains the structure and constraints that must be followed by the JSON instances of the data model described in the schema. The properties of the JSON Schema contain the vocabulary of the data model and are referred to as “keywords”.
Appendix A.2 describes in more detail the structure and keywords defined by the JSON Schema Draft-04 specification.