Operators

Operators used in Documotor transformations

Operators are constructs that behave like functions, but differ syntactically, namely, they are represented by symbols.

Comparison Operators

These mathematical operators take two arguments, one on each side of the symbol and return true if the expression is true, and false otherwise. The following operations are supported:

  • ==, tests for equality.
  • !=, tests for inequality.
  • <, less than.
  • <=, less than or equal to.
  • >, greater than.
  • >=, greater than or equal to.

Note that == and != are valid for inputs of any data types, but the other ones are limited to numbers and strings. Strings are compared alphabetically. Here's an example of the usage - data contains input pairs, and the map function applies the operator to the input pair. The output is a list of results of applying the operator to all input pairs in order.

{
  "compare": [
    [1, 1],
    [1, 2],
    ["ab", "cde"],
    [null, null],
    [{}, {}],
    [[1], [1, 2]]
  ]
}
{
  equality: map(&@[0]==@[1], compare),            // is 1==1?
  inequality: map(&@[0]!=@[1], compare),          // is 1!=1?
  lessThan: map(&@[0]<@[1], compare),             // is 1<1?
  lessThanOrEqual: map(&@[0]<=@[1], compare),     // is 1<=1?
  greaterThan: map(&@[0]>@[1], compare),          // is 1>1?
  greaterThanOrEqual: map(&@[0]>=@[1], compare)   // is 1>=1?
}
{
  "equality":[true, false, false, false, true, false],
  "inequality":[false, true, true, true, false, true],
  "lessThan":[false, true, true, null, null, null],
  "lessThanOrEqual":[true, true, true, null, null, null],
  "greaterThan":[false, false, false, null, null, null],
  "greaterThanOrEqual":[true, false, false, null, null, null]
}

Logical operators

There are three logical operators in JMESPath, from weakest to strongest precedence:

  • or: ||
  • and: &&
  • unary not: !

|| and && take two expressions as parameters, one on each side of the operator, denoted as &left and &right.

The result of the or expression is either &left or &right. If &left is truthy, return &left, otherwise, return &right. The consequence of this is that || returns a truthy value if one of the arguments is truthy, and a falsy value otherwise.

The result of the and expression is either &left or &right. If &left is falsy, return &left, otherwise, return &right. The consequence of this is that and returns a truthy value if both of the arguments are truthy, and a falsy value otherwise.

! takes one argument and returns the truth value opposite of the truth value of the argument.

{
  a: `{}` || `null`,
  b: `null` || `1`,
  c: '' && `1`,
  d: !`true`,
  e: `false` || `true`,
  f: `false` && `true`,
  g: !'true' || !`123`,
  h: `[]` || `null`
}
{
  "a": null,
  "b": 1,
  "c": "",
  "d": false,
  "e": true,
  "f": false,
  "g": {},
  "h": null
}