Product SiteDocumentation Site

Chapter 4. The JSONiq Type System

4.1. Item Types
4.1.1. Atomic Types
4.1.2. JSON Item Types : Object Types and Array Types
4.1.3. The Most General Item Type.
4.2. Sequence Types
JSONiq manipulates semi-structured data: in general, JSONiq allows you, but does not require you to specify types. So you have as much or as little type verification as you wish.
Like in Java or C++, it is possible to create a variable with a given static type:
Example 4.1. Specifying a type.
let $x as integer := 16
return $x * $x
Results:
256

Like in JavaScript, it is possible to create a variable without explicitly giving any static type. JSONiq is still strongly typed, so that you will be told if there is a type inconsistency or mismatch in your programs.
Example 4.2. Not specifying a type.
let $x := 16
return $x * $x
Results:
256

Variables are explained in Section 10.1, “Variables” in Chapter 10, FLWOR Expressions more in details.
JSONiq supports types at the sequence level. They are called sequence types, and the syntax for designing types is called the sequence type syntax. The type "integer" that was shown in Example 4.1, “Specifying a type.” matches singleton sequences of one atomic item of type integer.
We say that a sequence matches a sequence type (or that a sequence type matches a sequence) if the sequence is in the value space of the sequence type. Since an item is a particular (singleton) sequence, we also can say that an item matches an item type or conversely.
Whenever you do not specify the type of a variable or the type signature of a function, the most general type for any sequence of items, item*, is assumed. But it is not forbidden for the processor to be smart and warn you if it can detect that a type issue can arise at runtime.
There are many JSONiq expressions (cast, instance of, ...) which perform type operations and that make use of the sequence type syntax. In the remainder of this section, we will introduce sequence types using an "instance of" expression that returns true or false depending on whether or not the type on the right side is matched by the value on the left side -- like in Java.
Example 4.3. The instance of operator.
16 instance of integer
Results:
true

4.1. Item Types

4.1.1. Atomic Types

Atomic types are organized in a tree hierarchy.
JSONiq defines the following build-in types that have a direct relation with JSON:
  • string: the value space is all strings made of Unicode characters.
    All string literals build an atomic that matches string.
  • integer: the value space is that of all mathematical integral numbers (N), with an infinite range. This is a subtype of decimal, so that all integers also match the item type decimal.
    All integer literals build an atomic that matches integer.
  • decimal: the value space is that of all mathematical decimal numbers (D), with an infinite range.
    All decimal literals build an atomic that matches decimal.
  • double: the value space is that of all IEEE double-precision 64-bit floating point numbers.
    All double literals build an atomic that matches double.
  • boolean: the value space contains the booleans true and false.
    All boolean literals build an atomic that matches boolean.
  • null: the value space is a singleton and only contains null.
    All null literals build an atomic that matches null.
  • atomic: all atomic types.
    All literals build an atomic that matches atomic.
Example 4.4. Atomic types
16 instance of integer,
16 instance of decimal,
16.6 instance of decimal,
16.6e10 instance of double,
"foo" instance of string,
true instance of boolean,
null instance of null,
"foo" instance of atomic
Results:
true
true
true
true
true
true
true
true

JSONiq also supports further atomic types, which were borrowed from XML Schema 1.1.
These datatypes are already used as a set of atomic datatypes by the other two semi-structured data formats of the Web: XML and RDF, as well as by the corresponding query languages: XQuery and SPARQL, so it is natural for a complete JSON data model to reuse them.
  • Further number types: long, int, short, byte, float.
  • Date or time types: date, dateTime, dateTimeStamp, gDay, gMonth, gMonthDay, gYear, gYearMonth, time.
  • Duration types: duration, dayTimeDuration, yearMonthDuration.
  • Binary types: base64Binary, hexBinary.
  • An URI type: anyURI.
Atomic items that have these builtin atomic types can only be built with a constructor -- again similar to JavaScript.
Example 4.5. Further builtin atomic types.
date("2013-06-18") instance of date,
dateTime("2013-06-21T05:00:00Z") instance of dateTime,
time("05:00:00") instance of time,
long("1234567890123") instance of long
Results:
true
true
true
true

4.1.2. JSON Item Types : Object Types and Array Types

All objects match the item type object as well as json-item.
All arrays match the item type array as well as json-item.
Atomics do not match json-item.
Example 4.6. Further builtin atomic types.
{ "foo" : "bar" } instance of object,
{ "foo" : "bar" } instance of json-item,
{} instance of object,
[ 1, 2, 3, 4 ] instance of array,
[ 1, 2, 3, 4 ] instance of json-item
Results:
true
true
true
true
true

4.1.3. The Most General Item Type.

All items match the item type item.
Example 4.7. The most general item type: item.
{ "foo" : "bar" } instance of item,
[ 1, 2, 3, 4 ] instance of item,
"foo" instance of item,
42 instance of item,
false instance of item,
null instance of item
Results:
true
true
true
true
true
true