Skip to main content Skip to complementary content

avpath - Predicates

avpath predicates allow you to write very specific rules about items you want to select when constructing your expressions. Predicates are filters that restrict the items selected by location path. There are two possible types of predicates: object and positional.

For more information on location path, see avpath - Location path.

Object predicates

Object predicates can be used in a path expression to filter a subset of items according to boolean expressions working on the properties of each item. Object predicates are embedded in braces.

Basic expressions in object predicates:
  • numeric literals (e.g. 1.23)

  • string literals (e.g. "John Gold")

  • boolean literals (true/false)

  • subpathes (e.g. .nestedProp.deeplyNestedProp)

avpath allows you to use the following types of operators in predicate expressions:
  • comparison operators

  • string comparison operators

  • logical operators

  • arithmetic operators

Comparison operators
Operator Description Example

==

Returns true if both operands are equal

.customers{.id == "1"}

===

Returns true if both operands are strictly equal with no type conversion

.customers{.id === 1}

!=

Returns true if the operands are not equal

.customers{.id != "1"}

!==

Returns true if the operands are not equal and/or not of the same type

.customers{.id !== 1}

>

Returns true if the left operand is greater than the right operand

.customers{.id > 1}

>=

Returns true if the left operand is greater than or equal to the right operand

.customers{.id >= 1}

<

Returns true if the left operand is less than the right operand

.customers{.id < 1}

<=

Returns true if the left operand is less than or equal to the right operand

.customers{.id <= 1}

Comparison rules:
  • if both operands to be compared are arrays, then the comparison will be true if there is an element in the first array and an element in the second array such that the result of performing the comparison of two elements is true
  • if one operand is array and another is not, then the comparison will be true if there is element in array such that the result of performing the comparison of element and another operand is true
  • primitives will be compared as usual javascript primitives

If both operands are strings, additional comparison operators are available:

String comparison operators
Operator Description Example

==

Like a usual == but case insensitive

.customers{.Lastname == "Von Celaeno"}

^==

Returns true if left operand value begins with right operand value

.customers{.Lastname ^== "Celaeno"}

^=

Like the ^== but case insensitive

.customers{.Lastname ^= "celaeno"}

$==

Returns true if left operand value ends with right operand value

.customers{.Lastname $== "Von"}

$=

Like the $== but case insensitive

.customers{.Lastname $= "von"}

*==

Returns true if left operand value contains right operand value

.customers{.Lastname \*== "Celaeno"}

*=

Like the *== but case insensitive

.customers{.Lastname \*= "celaeno"}

Logical operators
Operator Description Example

&&

Returns true if both operands are true

.customers{.Revenue > 77000 && .customers.Lastname === "Von Celaeno"}

||

Returns true if either operand is true

.customers{.Firstname === "Quentin" || .State === "AZ"}

!

Negates the operand

.customers{!.Firstname}

Logical operators convert their operands to boolean values:
  • if operand is array (as the result of applying subpath is also array):
    • if length of array is greater than zero, the result will be true
    • else the result will be false
  • by casting with double NOT (!!) javascript operator to be used in any other cases.
Arithmetic operators
Operator Description

+

addition

-

subtraction

*

multiplication

/

division

%

modulus

Operator precedence
Operator Description

1 (top)

! -unary

2

* / %

3

+ -binary

4

>=

5

== === != !== ^= ^== $== $= *= *==

6

&&

7

||

Parentheses are used to explicitly denote precedence by grouping parts of an expression that should be evaluated first.

Examples:

// find the last name of the customer who lives in South Roosevelt Drive
avpath.select(doc, ".customers{.Address.Street === \"South Roosevelt Drive\"}.Lastname", schema)
// ['Novo']

// find all States where the customers revenue is less than 78000
avpath.select(doc, ".customers{.Revenue < 78000}.States", schema)
// ['AZ', 'CT']

Array positional predicates

Positional predicates allow you to filter items by their context position. Positional predicates are always embedded in square brackets.

There are four available forms:
  • [index] — returns index-positioned item in context (first item is at index 0), e.g. [3] returns fourth item in context

  • [index:] — returns items whose index in context is greater or equal to index, e.g. [2:] returns items whose index in context is greater or equal to 2

  • [:index] — returns items whose index in context is smaller than index, e.g. [:5] returns first five items in context

  • [indexFrom:indexTo] — returns items whose index in context is greater or equal to indexFrom and smaller than indexTo, e.g. [2:5] returns three items with indices 2, 3 and 4

You can also use negative position numbers:
  • [-1] — returns last item in context

  • [-3:] — returns last three items in context

Examples:

// find first customer state
avpath.select(doc, ".customers[0].States")
// ['AZ']

// find last customer state
avpath.select(doc, ".customers[-1].States")
// ['CT']

// find two customer states from second position
avpath.select(doc, ".customers[1:3].States")
// ['NC', 'CT']

Map key predicates

Key predicates allow you to filter items by their key using regular expressions (regex). Regex predicates are always embedded in ().
  • ("key" | ~"key regex") — returns items in context (which matches regex)

Multiple predicates

You can use more than one predicate. The result will contain only items that match all the predicates.

// find first customer name whose revenue less than 70000 and greater than 50000
avpath.select(doc, ".customers{.Revenue < 70000}{.Revenue > 50000}[0].Firstname")
// ['Quentin']

Did this page help you?

If you find any issues with this page or its content – a typo, a missing step, or a technical error – please let us know!