Product SiteDocumentation Site

Chapter 14. Function Library

JSONiq provides a rich set of builtin functions. We now introduce them, mostly by giving examples of usage.
Example 14.1. Functions on JSON data.
keys({ "foo" : 1, "bar" : 2 }),
members([ "mercury", "venus", "earth", "mars" ]),
parse-json(
"{ \"foo\" : \"bar\" }"
),
size([1 to 10]),
serialize({ "foo" : "bar" })
Results:
"foo"
"bar"
"mercury"
"venus"
"earth"
"mars"
{
  "foo" : "bar"
}
10
"{ "foo" : "bar" }"

Example 14.2. Miscellaneous functions.
collection("one-object"),
boolean("foo"),
if (1 + 1 ne 2) then error() else true
Results:
{
  "question" : "What NoSQL technology should I use?"
}
true
true

Example 14.3. Functions on numbers.
abs(-2.3),
ceiling(-2.3),
floor(-2.3),
round(-2.3),
round-half-to-even(-2.5145, 3),
number("3.14"),
format-integer(1234567, "000'111'222'333"),
format-number(1234567.8901234, "#,###.123")
Results:
2.3
-2
-3
-2
-2.514
3.14
"000'001'234'567"
"1,234,567.890"

Example 14.4. Functions on strings (1/2).
codepoints-to-string((78, 111, 83, 81, 76)),
string-to-codepoints("NoSQL"),
codepoint-equal(
    "NoSQL",
    "\u004E\u006F\u0053\u0051\u004C"
),
upper-case("NoSQL"),
lower-case("NoSQL"),
translate("NoSQL", "oN", "On"),
resolve-uri("types", "http://www.jsoniq.org/"),
encode-for-uri("1 + 1 is 2"),
iri-to-uri(
    "http://www.example.com/chuchichäschtli"),
escape-html-uri(
    "http://www.example.com/chuchichäschtli")
Results:
"NoSQL"
78
111
83
81
76
true
"NOSQL"
"nosql"
"nOSQL"
"http://www.jsoniq.org/types"
"1%20%2B%201%20is%202"
"http://www.example.com/chuchich%C3%A4schtli"
"http://www.example.com/chuchich%C3%A4schtli"

Example 14.5. Functions on strings (2/2).
concat("foo", 1, true, "bar", ()),
string-join((1 to 10) ! string($$), "-"),
string-length("123456789"),
contains("NoSQL", "SQL"),
starts-with("NoSQL", "No"),
ends-with("NoSQL", "SQL"),
substring("123456789", 5),
substring-before("NoSQL", "SQL"),
substring-after("NoSQL", "o"),
matches("NoSQL", "No[A-Z]+"),
replace("NoSQL", "No([A-Z])", "Yes$1"),
tokenize(
    "Go Boldly Where No Man Has Gone Before",
    " "
)
Results:
"foo1truebar"
"1-2-3-4-5-6-7-8-9-10"
9
true
true
true
"56789"
"No"
"SQL"
true
"YesSQL"
"Go"
"Boldly"
"Where"
"No"
"Man"
"Has"
"Gone"
"Before"

Example 14.6. Functions on sequences (1/2).
empty(("foo", "bar")),
exists(("foo", "bar")),
head(("foo", "bar")),
tail(("foo", "bar")),
[ insert-before(("foo", "bar"), 1, "foobar") ],
remove(("foo", "bar"), 1),
[ reverse(1 to 10) ],
[ subsequence(1 to 10, 2, 4) ],
unordered(("foo", "bar"))
Results:
false
true
"foo"
"bar"
[ "foobar", "foo", "bar" ]
"bar"
[ 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 ]
[ 2, 3, 4, 5 ]
"foo"
"bar"

Example 14.7. Functions on sequences (2/2).
distinct-values(
  ("foo", "bar", "foo", "bar", "foo")
),
index-of(
  ("foo", "bar", "foo", "bar", "foo"),
  "foo"),
deep-equal(
  { "foo" : [ 1 to 10 ] },
  { lower-case("FOO") : [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ] }
),
zero-or-one("foo"),
one-or-more(("foo", "bar")),
exactly-one("foo")
Results:
"foo"
"bar"
1
3
5
true
"foo"
"foo"
"bar"
"foo"

Example 14.8. Aggregation functions.
count(1 to 100),
avg(1 to 100),
max(1 to 100),
min(1 to 100),
sum(1 to 100)
Results:
100
50.5
100
1
5050

Example 14.9. Environment.
current-dateTime(),
current-date(),
current-time(),
implicit-timezone()
Results:
"2013-06-10T15:36:59.514267+02:00"
"2013-06-10+02:00"
"15:36:59.514267+02:00"
"PT2H"

Example 14.10. Constructors.
date("2013-06-21"),
time("17:00:00"),
dateTime("2013-06-21T17:00:00Z"),
dateTime("2013-06-21T17:00:00+01:00"),
duration("P2DT1H30M15S"),
hexBinary("511C7C5C9A277C22D138802F")
Results:
"2013-06-21"
"17:00:00"
"2013-06-21T17:00:00Z"
"2013-06-21T17:00:00+01:00"
"P2DT1H30M15S"
"511C7C5C9A277C22D138802F"