Objects

Expressions constructing objects are JSONiq-specific and introduced in this specification.

Figure 19. ObjectConstructor

ObjectConstructor

Figure 20. PairConstructor

PairConstructor

The syntax for creating objects is identical to that of JSON. You can use for an object key any string literal, and for an object value any literal, object constructor or array constructor.

Example 26. Empty object constructors

{}
      

Result: { }


Example 27. Object constructors 1

{ "foo" : "bar" }
      

Result: { "foo" : "bar" }


Example 28. Object constructors 2

{ "foo" : [ 1, 2, 3, 4, 5, 6 ] }
      

Result: { "foo" : [ 1, 2, 3, 4, 5, 6 ] }


Example 29. Object constructors 3

{ "foo" : true, "bar" : false }
      

Result: { "foo" : true, "bar" : false }


Example 30. Nested object constructors

{ "this is a key" : { "value" : "a value" } }
      

Result: { "this is a key" : { "value" : "a value" } }


As in JavaScript, if your key is simple enough (like alphanumerics, underscores, dashes, this kind of things), the quotes can be omitted. The strings for which quotes are not mandatory are called NCNames. This class of strings can be used for unquoted keys, for variable and function names, and for module aliases.

Example 31. Object constructors with unquoted key 1

{ foo : "bar" }
      

Result: { "foo" : "bar" }


Example 32. Object constructors with unquoted key 2

{ foo : [ 1, 2, 3, 4, 5, 6 ] }
      

Result: { "foo" : [ 1, 2, 3, 4, 5, 6 ] }


Example 33. Object constructors with unquoted key 3

{ foo : "bar", bar : "foo" }
      

Result: { "foo" : "bar", "bar" : "foo" }


Example 34. Object constructors with needed quotes around the key

{ "but you need the quotes here" : null }
    

Result: { "but you need the quotes here" : null }


Objects can be constructed more dynamically (e.g., dynamic keys) by constructing and merging smaller objects. Duplicate key names throw an error.

Example 35. Object constructors with needed quotes around the key

{|
  for $i in 1 to 3
  return { "foo" || $i : $i }
|}
    

Result: { "foo1" : 1, "foo2" : 2, "foo3" : 3 }