# JMESPath

> Query JSON with JMESPath through `$jmespath(data, query)`: filters, projections and the built-in functions listed here.

Source: https://docs.sommatic.ai/en/expressions/jmespath/

Version: 0.16.0 (Apache-2.0). Official documentation: https://jmespath.org/specification.html#built-in-functions

The first argument is the data, the second the query string:

```
{{ $jmespath($json, 'items[?status==`open`].id') }}
```

## Functions

### `abs()`

Returns the absolute value of a number. A non-number argument raises an `invalid-type` error.

```javascript
{{ $jmespath($json.body, 'abs(`-4.5`)') }}
```

Result (number):

```json
4.5
```

### `avg()`

Returns the average of an array of numbers, or `null` for an empty array. Every element must be a number. Example: `avg(items[*].price)`.

```javascript
{{ $jmespath($json.body, 'avg(items[*].price)') }}
```

Result (number):

```json
13.166666666666666
```

### `ceil()`

Returns the smallest integer greater than or equal to the given number. Example: `ceil(total)` returns `2` for `1.2`.

```javascript
{{ $jmespath($json.body, 'ceil(items[0].price)') }}
```

Result (number):

```json
11
```

### `contains()`

Returns `true` if an array contains an element equal to the search value, or if a string contains the search string as a substring. Example: `contains(tags, 'urgent')`.

```javascript
{{ $jmespath($json.body, 'contains(tags, `"web"`)') }}
```

Result (boolean):

```json
true
```

```javascript
{{ $jmespath($json.body, 'contains(email, `"@example.com"`)') }}
```

Result (boolean):

```json
true
```

### `ends_with()`

Returns `true` if the subject string ends with the given suffix, otherwise `false`. Example: `ends_with(file, '.pdf')`.

```javascript
{{ $jmespath($json.body, 'ends_with(email, `"example.com"`)') }}
```

Result (boolean):

```json
true
```

### `floor()`

Returns the largest integer less than or equal to the given number. Example: `floor(total)` returns `1` for `1.8`.

```javascript
{{ $jmespath($json.body, 'floor(items[0].price)') }}
```

Result (number):

```json
10
```

### `join()`

Concatenates an array of strings, placing the glue string between each element; the glue is the first argument. Every element must be a string. Example: `join(', ', tags)` returns `"a, b"`.

```javascript
{{ $jmespath($json.body, 'join(`", "`, items[*].sku)') }}
```

Result (string):

```json
"A-100, B-200, C-300"
```

### `keys()`

Returns an array with the keys of an object. Key order is not guaranteed.

```javascript
{{ $jmespath($json.body, 'keys(customer)') }}
```

Result (list):

```json
[
  "name",
  "segment"
]
```

### `length()`

Returns the number of code points in a string, the number of elements in an array, or the number of key-value pairs in an object. Any other type raises an `invalid-type` error.

```javascript
{{ $jmespath($json.body, 'length(items)') }}
```

Result (number):

```json
3
```

```javascript
{{ $jmespath($json.body, 'length(order_id)') }}
```

Result (number):

```json
8
```

### `map()`

Applies an expression reference to every element of an array and returns the results. Unlike a projection, `null` results are kept, so the output has the same length as the input. Example: `map(&name, people)`.

```javascript
{{ $jmespath($json.body, 'map(&qty, items)') }}
```

Result (list):

```json
[
  2,
  0,
  1
]
```

### `max()`

Returns the highest value in an array of numbers or an array of strings, or `null` for an empty array. Example: `max(items[*].price)`.

```javascript
{{ $jmespath($json.body, 'max(items[*].price)') }}
```

Result (number):

```json
25
```

### `max_by()`

Returns the element of an array for which the expression reference yields the highest value; the expression must return a number or a string. Example: `max_by(people, &age).name`.

```javascript
{{ $jmespath($json.body, 'max_by(items, &price).sku') }}
```

Result (string):

```json
"C-300"
```

### `merge()`

Merges zero or more objects into a single object. When several objects share a key, the value from the last one wins. Example: `merge(defaults, overrides)`.

```javascript
{{ $jmespath($json.body, 'merge(customer, `{"vip": true}`)') }}
```

Result (object):

```json
{
  "name": "Ana Pérez",
  "segment": "gold",
  "vip": true
}
```

### `min()`

Returns the lowest value in an array of numbers or an array of strings, or `null` for an empty array. Example: `min(items[*].price)`.

```javascript
{{ $jmespath($json.body, 'min(items[*].qty)') }}
```

Result (number):

```json
0
```

### `min_by()`

Returns the element of an array for which the expression reference yields the lowest value; the expression must return a number or a string. Example: `min_by(people, &age).name`.

```javascript
{{ $jmespath($json.body, 'min_by(items, &price).sku') }}
```

Result (string):

```json
"B-200"
```

### `not_null()`

Evaluates its arguments in order and returns the first one that is not `null`, or `null` if all of them are. Empty arrays and other falsy values count as not null. Example: `not_null(nickname, name)`.

```javascript
{{ $jmespath($json.body, 'not_null(shipping, customer.segment)') }}
```

Result (string):

```json
"gold"
```

### `reverse()`

Reverses the order of the elements of an array, or the characters of a string. Example: `reverse(items)`.

```javascript
{{ $jmespath($json.body, 'reverse(items[*].sku)') }}
```

Result (list):

```json
[
  "C-300",
  "B-200",
  "A-100"
]
```

### `sort()`

Sorts an array of numbers or an array of strings in ascending order; strings are compared by code point. Example: `sort(tags)`.

```javascript
{{ $jmespath($json.body, 'sort(tags)') }}
```

Result (list):

```json
[
  "new",
  "new",
  "web"
]
```

### `sort_by()`

Sorts an array by the value the expression reference returns for each element, which must be a number or a string. Example: `sort_by(people, &age)[*].name`.

```javascript
{{ $jmespath($json.body, 'sort_by(items, &price)[*].sku') }}
```

Result (list):

```json
[
  "B-200",
  "A-100",
  "C-300"
]
```

### `starts_with()`

Returns `true` if the subject string starts with the given prefix, otherwise `false`. Example: `starts_with(code, 'INV-')`.

```javascript
{{ $jmespath($json.body, 'starts_with(order_id, `"ord_"`)') }}
```

Result (boolean):

```json
true
```

### `sum()`

Returns the sum of an array of numbers, or `0` for an empty array. Every element must be a number. Example: `sum(items[*].amount)`.

```javascript
{{ $jmespath($json.body, 'sum(items[*].qty)') }}
```

Result (number):

```json
3
```

### `to_array()`

Returns an array unchanged and wraps any other value in a one-element array.

```javascript
{{ $jmespath($json.body, 'to_array(order_id)') }}
```

Result (list):

```json
[
  "ord_1042"
]
```

### `to_number()`

Returns a number unchanged and parses a string that holds a valid JSON number; any other value, including a string that is not a number, yields `null`.

```javascript
{{ $jmespath($json.body, 'to_number(`"42.5"`)') }}
```

Result (number):

```json
42.5
```

### `to_string()`

Returns a string unchanged and converts any other value to its JSON text. Example: `to_string(count)` returns `"2"` for `2`.

```javascript
{{ $jmespath($json.body, 'to_string(items[0].qty)') }}
```

Result (string):

```json
"2"
```

### `type()`

Returns the JSON type of a value as a string: `number`, `string`, `boolean`, `array`, `object` or `null`.

```javascript
{{ $jmespath($json.body, 'type(items)') }}
```

Result (string):

```json
"array"
```

### `values()`

Returns an array with the values of an object. Order is not guaranteed.

```javascript
{{ $jmespath($json.body, 'values(customer)') }}
```

Result (list):

```json
[
  "Ana Pérez",
  "gold"
]
```
