Having a JSON document as pure syntax is not very useful in itself, except to send it over a network or to store it in a document store of course. To make use of it in a database or in other processing environments, you need to bring it to a higher level of abstraction and give semantics to the building blocks. This is what a Data Model is for.
We now introduce the JSONiq data model.
Let us begin with some good news first: the JSON syntax that we have just introduced is a subset of JSONiq. Concretely, this means that any of these syntactic JSON building blocks can be copy-and-pasted, and executed as a JSONiq query. The output will be the counterpart of this JSON building block in the Data Model. So, if you are familiar with JSON, then you already know some JSONiq.
3.1. JSONiq Values: Items and Sequences
In JSONiq, the JSON building blocks described in the former section, on a more abstract level, are referred to as items. JSONiq manipulates sequences of these items. Hence, a JSONiq value is a sequence of items. So, in particular, a JSONiq query returns sequences of items. Actually, even inside a JSONiq query, sequences of items are passed around between the JSONiq building blocks internal to a query (called expressions).
Let us copy-and-paste a JSON Object and execute it as JSONiq:
Example 3.1. A sequence of just one item.
{ "foo" : "bar" }
Results:
{
"foo" : "bar"
}
The above query generates a sequence of one item, an object item in this case. The result displayed above is the output of this query when run with the Zorba query processor, which is one of the JSONiq implementations.
Commas are all you need to begin building your own sequences. You can mix and match!
There are three golden rules about sequences that are useful to keep in mind.
Rule #1: Sequences are flat and cannot be nested. This makes streaming possible, which is very powerful.
Example 3.3. Sequences are flat.
( ("foo", 2), ( (true, 4, null), 6 ) )
Results:
"foo"
2
true
4
null
6
Rule #2: A sequence can be empty. The empty sequence can be constructed with empty parentheses.
Example 3.4. The empty sequence.
()
Results:
Rule #3: A sequence of just one item is considered the same as this item itself. Whenever we say that an expression returns or takes one item, we really mean that it takes a singleton sequence of one item.
Example 3.5. A sequence of one item.
("foo")
Results:
"foo"
JSONiq classifies the items mentioned above in three categories:
Objects: the counterparts of the syntactic JSON objects.
Arrays: the counterparts of the syntactic JSON arrays.
Atomics: the counterparts of JSON strings, JSON numbers, JSON booleans and JSON nulls - but with a very rich type system which includes dates, for example.