Ternary operator - jmespath-community/jmespath.spec GitHub Wiki

Problem statement

Most languages have support for simple conditions:

if x then y else z

Popular languages also support combining a condition to return a particular value for assignment, using the ternary operator:

x ? y : z
  • Does JMESPath support the ternary operator?
  • How to assign based on a condition?

How to

JMESPath supports logical operators with Or Expression || and And Expressions &&.

JMESPath has a peculiar concept of falsy values. A falsy value corresponds to any of the following conditions:

  • Empty list: []
  • Empty object: {}
  • Empty string: ""
  • Boolean False: false
  • Null value: null

A truthy value corresponds to any value that is not false.

With that in mind, you can emulate the ternary operator like so:

(x && y) || z