Function library
Function Library
JSONiq provides a rich set of functions.
JSON specific functions.
Some functions are specific to JSON.
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*
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
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" }
members
This functions returns all members of all arrays of the supplied sequence.
members($a as item*) as item*
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
null
This function returns the JSON null.
null() as null
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.
Parsing a JSON document
parse-json("{ \"foo\" : \"bar\" }", { "jsoniq-multiple-top-level-items" : false })
Result (run with Zorba):{ "foo" : "bar" }
Parsing multiple, whitespace-separated JSON documents
parse-json("{ \"foo\" : \"bar\" } { \"bar\" : \"foo\" }")
Result (run with Zorba):{ "foo" : "bar" } { "bar" : "foo" }
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?
Retrieving the size of an array
let $a := [1 to 10]
return size($a)
Result (run with Zorba):10
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.$$ }
|}
};
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 descendant-arrays(values($i))
default return ()
};
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 descendant-objects($i[])
default return ()
};
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 ()
};
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
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
};
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 }
|}
};
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
};
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" }
Projecting an object 2
let $o := {
"Captain" : "Kirk",
"First Officer" : "Spock",
"Engineer" : "Scott"
}
return project($o, "XQuery Evangelist")
Result (run with Zorba):{ }
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
};
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
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)
};
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*
decode-from-roundtrip
This function decodes a sequence previously encoded with encode-for-roundtrip.
decode-from-roundtrip($items as json-item*) as item*
Functions taken from XQuery
Access to the external environment: collection#1
Function to turn atomics into booleans for use in two-valued logics: boolean#1
Functions on numeric values: abs#1, ceilingabs#1, floorabs#1, roundabs#1, round-half-to-even#1
Formatting integers: format-integer#2, format-integer#3
Formatting numbers: format-numberreturn r#2, format-number#3
Functions to assemble and disassemble strings: codepoints-to-string#1, string-to-codepoints#1
Comparison of strings: compare#2, compare#3, codepoint-equal#2
Functions on string values: concat#2, string-join#1, string-join#2, substring#2, substring#3, string-length#0, string-length#1, normalize-space#0, normalize-space#1, normalize-unicode#1, normalize-unicode#2, upper-case#1, lower-case#1, translate#3
Functions based on substring matching: contains#2, contains#3, starts-with#2, starts-with#3, ends-with#2, ends-with#3, substring-before#2, substring-before#3, substring-after#2, substring-after#3
String functions that use regular expressions: matches#2, matches#3, replace#3, replace#4, tokenize#2, tokenize#3
Functions that manipulate URIs: resolve-uri#1, resolve-uri#2, encode-for-uri#1, iri-to-uri#1, escape-html-uri#1
General functions on sequences: empty#1, exists#1, head#1, tail#1, insert-before#3, remove#2, reverse#1, subsequence#2, subsequence#3, unordered#1
Function that compare values in sequences: distinct-values#1, distinct-values#2, index-of#2, index-of#3, deep-equal#2. deep-equal#3
Functions that test the cardinality of sequences: zero-or-one#1, one-or-more#1, exactly-one#1
Serializing functions: serialize#1 (unary)
Context information: current-dateTime#1, current-date#1, current-time#1, implicit-timezone#1, default-collation#1
Constructor functions: for all builtin types, with the name of the builtin type and unary. Equivalent to a cast expression.
Last updated