Product SiteDocumentation Site

5.2. Object Constructors

The syntax for creating objects is also 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 5.5. Object constructors.
{},
{ "foo" : "bar" },
{ "foo" : [ 1, 2, 3, 4, 5, 6 ] },
{ "foo" : true, "bar" : false },
{ "this is a key" : { "value" : "a value" } }
Results:
{
}
{
  "foo" : "bar"
}
{
  "foo" : [ 1, 2, 3, 4, 5, 6 ]
}
{
  "foo" : true,
  "bar" : false
}
{
  "this is a key" : {
    "value" : "a value"
  }
}

Again, JSONiq is more flexible here. Like in JavaScript, if your key is simple enough (like alphanumerics, underscores, dashes, these kinds of things), you are welcome to omit the quotes. The strings for which quotes are not mandatory are called unquoted names. This class of strings can be used for unquoted keys, but also in later sections for variable and function names, and for module aliases.
Example 5.6. Object constructors with unquoted keys.
{ foo : "bar" },
{ foo : [ 1, 2, 3, 4, 5, 6 ] },
{ foo : "bar", bar : "foo" },
{ "but you need the quotes here" : null }
Results:
{
  "foo" : "bar"
}
{
  "foo" : [ 1, 2, 3, 4, 5, 6 ]
}
{
  "foo" : "bar",
  "bar" : "foo"
}
{
  "but you need the quotes here" : null
}