Product SiteDocumentation Site

Chapter 10. Notes

10.1. Sequences vs. Arrays
10.2. Null vs. empty sequence

10.1. Sequences vs. Arrays

Even though JSON supports arrays, JSONiq uses a different construct as its first class citizens: sequences. Any value returned by or passed to an expression is a sequence.
The main difference between sequences and arrays is that sequences are completely flat, meaning they cannot contain other sequences.
Since sequences are flat, expressions of the JSONiq language just concatenate them to form bigger sequences.
This is crucial to allow streaming results, for example through an HTTP session.
Example 10.1. Flat sequences
( (1, 2), (3, 4) )
Result (run with Zorba):
1 2 3 4

Arrays on the other side can contain nested arrays, like in JSON.
Example 10.2. Nesting arrays
[ [ 1, 2 ], [ 3, 4 ] ]
Result (run with Zorba):
[ [ 1, 2 ], [ 3, 4 ] ]

Many expressions return single items - actually, they really return a singleton sequence, but a singleton sequence of one item is considered the same as this item.
Example 10.3. Singleton sequences
1 + 1
Result (run with Zorba):
2

This is different for arrays: a singleton array is distinct from its unique member, like in JSON.
Example 10.4. Singleton sequences
[ 1 + 1 ]
Result (run with Zorba):
[ 2 ]

An array is a single item. A (non-singleton) sequence is not. This can be observed by counting the number of items in a sequence.
Example 10.5. count() on an array
count([ 1, "foo", [ 1, 2, 3, 4 ], { "foo" : "bar" } ])
Result (run with Zorba):
1

Example 10.6. count() on a sequence
count( ( 1, "foo", [ 1, 2, 3, 4 ], { "foo" : "bar" } ) )
Result (run with Zorba):
4

Other than that, arrays and sequences can contain exactly the same members (atomics, arrays, objects).
Example 10.7. Members of an array
[ 1, "foo", [ 1, 2, 3, 4 ], { "foo" : "bar" } ]
Result (run with Zorba):
[ 1, "foo", [ 1, 2, 3, 4 ], { "foo" : "bar" } ]

Example 10.8. Members of an sequence
( 1, "foo", [ 1, 2, 3, 4 ], { "foo" : "bar" } )
Result (run with Zorba):
1 foo [ 1, 2, 3, 4 ] { "foo" : "bar" }

Arrays can be converted to sequences, and vice-versa.
Example 10.9. Converting an array to a sequence
[ 1, "foo", [ 1, 2, 3, 4 ], { "foo" : "bar" } ] []
Result (run with Zorba):
1 foo [ 1, 2, 3, 4 ] { "foo" : "bar" }

Example 10.10. Converting a sequence to an array
[ ( 1, "foo", [ 1, 2, 3, 4 ], { "foo" : "bar" } ) ]
Result (run with Zorba):
[ 1, "foo", [ 1, 2, 3, 4 ], { "foo" : "bar" } ]