d2path examples¶
A validated gallery of d2path expressions grouped by category. Every expression, its input, and its result are checked by the test suite (packages/dhis2w-ql/tests/test_doc_examples.py) and rendered from dhis2w_ql.doc_examples.DOC_EXAMPLES — do not edit by hand. Rebuild via make docs-d2path (chained into make docs-build).
Total examples: 140.
Filtering & projection¶
where()¶
Keep the matching sub-object, then navigate into it.
Input:
{
"name": [
{
"use": "official",
"given": [
"Ada",
"Lovelace"
],
"family": "King"
},
{
"use": "nick",
"given": [
"Countess"
]
}
]
}
Result:
Filter a collection of objects by a numeric predicate on $this.
Input:
Result:
Select one telecom channel by its system, then read its value.
Input:
Result:
Filter data elements by valueType, then read the surviving names.
Input:
{
"elements": [
{
"name": "Malaria cases",
"valueType": "NUMBER"
},
{
"name": "Clinical notes",
"valueType": "TEXT"
}
]
}
Result:
select()¶
Reshape each item into a new object with an object constructor.
Input:
Result:
Map every item through an arithmetic expression on $this.
Input:
Result:
Project and transform a field from each item.
Input:
Result:
Rename organisation-unit fields into a compact projection.
Input:
Result:
iif()¶
Return one of two values based on a predicate.
Input:
Result:
Branch on a boolean field.
Input:
Result:
Threshold a numeric field into a label.
Input:
Result:
Label a coverage value that clears a target.
Input:
Result:
Existence & logic¶
exists()¶
With no argument, report whether the collection is non-empty.
Input:
Result:
An empty (or missing) collection does not exist.
Input:
Result:
With a predicate, report whether any item satisfies it.
Input:
Result:
all()¶
True when every item satisfies the predicate.
Input:
Result:
A single failing item makes all false.
Input:
Result:
empty()¶
An empty collection is empty.
Input:
Result:
A non-empty collection is not empty.
Input:
Result:
A missing field navigates to the empty collection, which is empty.
Input:
Result:
not()¶
Negate a boolean field.
Input:
Result:
Negate a true flag.
Input:
Result:
An empty focus is falsy, so not() returns true.
Input:
Result:
Subsetting & set operations¶
first()¶
Keep the first item of a collection.
Input:
Result:
Take the first name after navigating a repeated field.
Input:
Result:
last()¶
Keep the last item of a collection.
Input:
Result:
Read the most recent period from an ordered list.
Input:
Result:
tail()¶
Drop the first item, keeping the rest.
Input:
Result:
The tail of a single-element collection is empty.
Input:
Result:
skip()¶
Drop the first N items.
Input:
Result:
Skipping past the end yields the empty collection.
Input:
Result:
take()¶
Keep the first N items.
Input:
Result:
Taking zero items yields the empty collection.
Input:
Result:
count()¶
Count the items in a collection.
Input:
Result:
Count a repeated field after navigation.
Input:
Result:
distinct()¶
Drop duplicate strings, preserving first-seen order.
Input:
Result:
De-duplicate a numeric collection.
Input:
Result:
Chain into count() to count the unique values.
Input:
Result:
isDistinct()¶
True when a collection has no duplicates.
Input:
Result:
Repeated organisation-unit levels are not distinct.
Input:
Result:
union()¶
Union with itself collapses to the distinct set.
Input:
Result:
Merge in a projected collection, de-duplicating the result.
Input:
Result:
combine()¶
Append another collection, keeping duplicates (unlike union).
Input:
Result:
Concatenating a collection with a copy of itself doubles it.
Input:
Result:
Strings¶
substring()¶
Take a fixed-length slice from a start offset.
Input:
Result:
Omit the length to slice to the end of the string.
Input:
Result:
Slice a prefix for grouping or matching.
Input:
Result:
A length past the end of the string clips to what remains.
Input:
Result:
upper()¶
Upper-case a string.
Input:
Result:
A non-string (or missing) focus yields the empty collection.
Input:
Result:
lower()¶
Lower-case a string.
Input:
Result:
Normalise a code to lower case.
Input:
Result:
length()¶
Report a string's character count.
Input:
Result:
A missing field yields the empty collection, not zero.
Input:
Result:
trim()¶
Strip leading and trailing whitespace.
Input:
Result:
Trim removes tabs and newlines too.
Input:
Result:
toChars()¶
Explode a string into its characters.
Input:
Result:
A single-character string becomes a one-element list.
Input:
Result:
startsWith()¶
Test a string prefix.
Input:
Result:
A non-matching prefix is false.
Input:
Result:
endsWith()¶
Test a string suffix.
Input:
Result:
A non-matching suffix is false.
Input:
Result:
contains()¶
Test whether a string contains a substring (method form).
Input:
Result:
A missing substring is not contained.
Input:
Result:
indexOf()¶
Find the zero-based offset of a substring.
Input:
Result:
A substring that is absent returns -1.
Input:
Result:
replace()¶
Replace every occurrence of a literal substring.
Input:
Result:
Swap a separator character in a code.
Input:
Result:
matches()¶
Test a string against a regular expression.
Input:
Result:
A pattern that does not match returns false.
Input:
Result:
split()¶
Split a string on a separator into a collection.
Input:
Result:
A separator that is absent yields the whole string as one element.
Input:
Result:
join()¶
Join a collection with a separator.
Input:
Result:
Build a comma-separated label from tags.
Input:
Result:
Math & aggregates¶
sum()¶
Sum a numeric collection (aggregates return decimals).
Input:
Result:
Analytics values arrive as strings; sum coerces them to numbers.
Input:
Result:
min()¶
Smallest value in a numeric collection.
Input:
Result:
Minimum over string-typed analytics values.
Input:
Result:
max()¶
Largest value in a numeric collection.
Input:
Result:
Maximum over string-typed analytics values.
Input:
Result:
avg()¶
Arithmetic mean of a numeric collection.
Input:
Result:
Average of string-typed analytics values.
Input:
Result:
abs()¶
Absolute value of a number.
Input:
Result:
Absolute value of a scalar focus.
Input:
Result:
round()¶
Round to a given number of decimal places.
Input:
Result:
Round a coverage figure to one decimal place.
Input:
Result:
Omitting the precision rounds to the nearest whole number.
Input:
Result:
Conversion & temporal¶
toInteger()¶
Parse a numeric string into an integer.
Input:
Result:
A decimal is truncated toward zero.
Input:
Result:
toDecimal()¶
Parse a numeric string into a decimal.
Input:
Result:
Widen an integer into a decimal.
Input:
Result:
toString()¶
Render a number as a string.
Input:
Result:
A boolean renders as the canonical true/false text.
Input:
Result:
A null (or missing) focus yields the empty collection.
Input:
Result:
today()¶
Current date as an ISO-8601 string; the exact value varies at runtime.
Result (varies at runtime; shape shown):
An ISO date is always ten characters long.
Result:
Slice the year out of today's date; the value varies at runtime.
Result (varies at runtime; shape shown):
now()¶
Current timestamp as an ISO-8601 string; the exact value varies at runtime.
Result (varies at runtime; shape shown):
Slice the date portion out of the current timestamp; the value varies at runtime.
Result (varies at runtime; shape shown):
Operators¶
operator-implies¶
a implies b is false only when a holds but b does not.
Input:
Result:
A false antecedent makes implies vacuously true.
Input:
Result:
A true antecedent with a true consequent holds.
Input:
Result:
operator-in¶
Test membership of a scalar in an array literal.
Input:
Result:
A value outside the set is not a member.
Input:
Result:
operator-concat¶
+ concatenates two strings.
Input:
Result:
Prefix a field with a string literal to build a label.
Input:
Result:
Build an analytics dimension key from fields.
Input:
Result:
operator-arithmetic¶
/ is true division and keeps the fraction.
Result:
div is integer division, truncating toward zero.
Result:
mod is the remainder.
Result:
Unary minus negates a number (arithmetic is decimal-valued).
Input:
Result:
operator-xor¶
xor is true when exactly one side holds.
Input:
Result:
Both sides true is not exclusive, so xor is false.
Input:
Result:
operator-not-match¶
!~ is true when the case-insensitive substring is absent.
Input:
Result:
~ matches case-insensitively, so !~ is false when it would match.
Input:
Result:
operator-contains¶
Infix contains tests membership in a collection.
Input:
Result:
A value not in the collection is not contained.
Input:
Result:
With a string on both sides, infix contains is a substring test.
Input:
Result:
operator-is¶
is Integer tests for an integer value.
Input:
Result:
is Decimal tests for a floating-point value.
Input:
Result:
is String tests for a string value.
Input:
Result:
is Boolean tests for a boolean value.
Input:
Result:
is Object tests for a structured node.
Input:
Result:
A numeric string is a String, not an Integer.
Input:
Result:
existential-comparison¶
> over a repeated field is true when any item exceeds the scalar.
Input:
Result:
= holds when any value in the collection matches.
Input:
Result:
!= means no pair is equal, so a present match makes it false.
Input:
Result:
< holds when some value falls below the scalar.
Input:
Result:
~ holds when any value matches case-insensitively.
Input:
Result:
!~ means no value matches, so a present match makes it false.
Input:
Result:
Literals & navigation¶
literal-date¶
A @-prefixed date literal evaluates to its ISO-8601 string.
Result:
Compare an event date against a date literal (ISO strings order lexically).
Input:
Result:
A date literal equals a matching ISO date string.
Input:
Result:
literal-datetime¶
A @-prefixed datetime literal evaluates to its ISO-8601 string.
Result:
Compare a timestamp field against a datetime literal.
Input:
Result:
path-navigation¶
A positive integer subscript indexes a collection.
Input:
Result:
An out-of-bounds index yields the empty collection, not an error.
Input:
Result:
An integer-valued index may be negative, counting from the end.
Input:
Result:
A string subscript reaches keys that are not identifiers.
Input:
Result:
Each navigation hop flattens one level of nested collections.
Input:
Result: