Product SiteDocumentation Site

Chapter 8. Function Library

8.1. JSON specific functions.
8.1.1. keys
8.1.2. members
8.1.3. null
8.1.4. parse-json
8.1.5. size
8.1.6. accumulate
8.1.7. descendant-arrays
8.1.8. descendant-objects
8.1.9. descendant-pairs
8.1.10. flatten
8.1.11. intersect
8.1.12. project
8.1.13. remove-keys
8.1.14. values
8.1.15. encode-for-roundtrip
8.1.16. decode-from-roundtrip
8.2. Functions taken from XQuery
JSONiq provides a rich set of functions.

8.1. JSON specific functions.

Some functions are specific to JSON.

8.1.1. keys

This function returns the distinct keys of all objects in the supplied sequence, in an implementation-dependent order.
keys($o as item*) as string*
Example 8.1. Getting all distinct key names in the supplied objects, ignoring non-objects.
let $o := ("foo", [ 1, 2, 3 ], { "a" : 1, "b" : 2 }, { "a" : 3, "c" : 4 })
return keys($o)
Result (run with Zorba):
a b c

Example 8.2. Retrieving all Pairs from an Object:
let $map := { "eyes" : "blue", "hair" : "fuchsia" }
for $key in keys($map)
return { $key : $map.$key }
Result (run with Zorba):
{ "eyes" : "blue" } { "hair" : "fuchsia" }

8.1.2. members

This functions returns all members of all arrays of the supplied sequence.
members($a as item*) as item*
Example 8.3. Retrieving the members of all supplied arrays, ignoring non-arrays.
let $planets :=  ( "foo", { "foo" : "bar "}, [ "mercury", "venus", "earth", "mars" ], [ 1, 2, 3 ])
return members($planets)
Result (run with Zorba):
mercury venus earth mars 1 2 3

8.1.3. null

This function returns the JSON null.
null() as null

8.1.4. parse-json

This function parses its first parameter (a string) as JSON, and returns the resulting sequence of objects and arrays.
parse-json($arg as string?) as json-item*
parse-json($arg as string?, $options as object) as json-item*
The object optionally supplied as the second parameter may contain additional options:
  • jsoniq-multiple-top-level-items (boolean): indicates whether parsing to zero, or several objects is allowed. An error is raised if this value is false and there is not exactly one object that was parsed.
If parsing is not successful, an error is raised. Parsing is considered in particular to be non-successful if the boolean associated with "jsoniq-multiple-top-level-items" in the additional parameters is false and there is extra content after parsing a single abject or array.
Example 8.4. Parsing a JSON document
parse-json("{ \"foo\" : \"bar\" }", { "jsoniq-multiple-top-level-items" : false })
Result (run with Zorba):
{ "foo" : "bar" }

Example 8.5. Parsing multiple, whitespace-separated JSON documents
parse-json("{ \"foo\" : \"bar\" } { \"bar\" : \"foo\" }")
Result (run with Zorba):
{ "foo" : "bar" } { "bar" : "foo" }

8.1.5. size

This function returns the size of the supplied array, or the empty sequence if the empty sequence is provided.
size($a as array?) as integer?
Example 8.6. Retrieving the size of an array
let $a := [1 to 10]
return size($a)
Result (run with Zorba):
10

8.1.6. accumulate

This function dynamically builds an object, like the {| |} syntax, except that it does not throw an error upon pair collision. Instead, it accumulates them, wrapping into an array if necessary. Non-objects are ignored.
declare function accumulate($seq as item*) as object
{
  {|
    keys($seq) ! { $$ : $seq.$$ }
  |}
};

8.1.7. descendant-arrays

This function returns all arrays contained within the supplied items, regardless of depth.
declare function descendant-arrays($seq as item*) as array*
{
  for $i in $seq
  return typeswitch ($i)
  case array return ($i, descendant-arrays($i[])
  case object return return descendant-arrays(values($i))
  default return ()
};

8.1.8. descendant-objects

This function returns all objects contained within the supplied items, regardless of depth.
declare function descendant-objects($seq as item*) as object*
{
  for $i in $seq
  return typeswitch ($i)
  case object return ($i, descendant-objects(values($i)))
  case array return return descendant-objects($i[])
  default return ()
};

8.1.9. descendant-pairs

This function returns all descendant pairs within the supplied items.
declare function descendant-pairs($seq as item*)
{
  for $i in $seq
  return typeswitch ($i)
  case object return
    for $k in keys($o)
    let $v := $o.$k
    return ({ $k : $v }, descendant-pairs($v))
  case array return descendant-pairs($i[])
  default return ()
};
Example 8.7. Accessing all descendant pairs
let $o := 
{
  "first" : 1,
  "second" : { 
    "first" : "a", 
    "second" : "b" 
  }
}
return descendant-pairs($o)
Result (run with Zorba):
An error was raised: "descendant-pairs": function with arity 1 not declared

8.1.10. flatten

This function recursively flattens arrays in the input sequence, leaving non-arrays intact.
declare function flatten($seq as item*) as item*
{
  for $value in $seq
  return typeswitch ($value)
         case array return flatten($value[])
         default return $value
};

8.1.11. intersect

This function returns the intersection of the supplied objects, and aggregates values corresponding to the same name into an array. Non-objects are ignored.
declare function intersect($seq as item*)
{
  {|
    let $objects := $seq[. instance of object()]
    for $key in keys(head($objects))
    where every $object in tail($objects)
          satisfies exists(index-of(keys($object), $key))
    return { $key : $objects.$key }
  |}
};

8.1.12. project

This function iterates on the input sequence. It projects objects by filtering their pairs and leaves non-objects intact.
declare function project($seq as item*, $keys as string*) as item*
{
  for $item in $seq
  return typeswitch ($item)
         case $object as object return
         {|
           for $key in keys($object)
           where some $to-project in $keys satisfies $to-project eq $key
           let $value := $object.$key
           return { $key : $value }
         |}
         default return $item
};
Example 8.8. Projecting an object 1
let $o := {
  "Captain" : "Kirk",
  "First Officer" : "Spock",
  "Engineer" : "Scott"
  }
return project($o, ("Captain", "First Officer"))
Result (run with Zorba):
{ "Captain" : "Kirk", "First Officer" : "Spock" }

Example 8.9. Projecting an object 2
let $o := {
  "Captain" : "Kirk",
  "First Officer" : "Spock",
  "Engineer" : "Scott"
  }
return project($o, "XQuery Evangelist")
Result (run with Zorba):
{ }

8.1.13. remove-keys

This function iterates on the input sequence. It removes the pairs with the given keys from all objects and leaves non-objects intact.
declare function remove-keys($seq as item*, $keys as string*) as item*
{
  for $item in $seq
  return typeswitch ($item)
         case $object as object return
         {|
           for $key in keys($object)
           where every $to-remove in $keys satisfies $to-remove ne $key
           let $value := $object.$key
           return { $key : $value }
         |}
         default return $item
};
Example 8.10. Removing keys from an object (not implemented yet)
let $o := {
  "Captain" : "Kirk",
  "First Officer" : "Spock",
  "Engineer" : "Scott"
  }
return remove-keys($o, ("Captain", "First Officer"))
Result (run with Zorba):
An error was raised: "remove-keys": function with arity 2 not declared

8.1.14. values

This function returns all values in the supplied objects. Non-objects are ignored.
declare function values($seq as item*) as item* {
  for $i in $seq
  for $k in jn:keys($i)
  return $i($k)
};

8.1.15. encode-for-roundtrip

This function encodes any sequence of items, even containing non-JSON types, to a sequence of JSON items that can be serialized as pure JSON, in a way that it can be parsed and decoded back using decode-from-roundtrip. JSON features are left intact, while atomic items annotated with a non-JSON type are converted to objects embedding all necessary information.
encode-for-roundtrip($items as item*) as json-item*

8.1.16. decode-from-roundtrip

This function decodes a sequence previously encoded with encode-for-roundtrip.
decode-from-roundtrip($items as json-item*) as item*