Skip to content

Commit

Permalink
document jsonRemove handlebars helper.
Browse files Browse the repository at this point in the history
  • Loading branch information
RafeArnold committed Nov 22, 2024
1 parent 7d1f695 commit ff44bed
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions _docs/response-templating.md
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,82 @@ Like the `jsonArrayAdd` helper, the second object can be provided as a block:

{% endraw %}

## Removing from a JSON Array or Object

The `jsonRemove` helper allows you to remove an element from an existing json array, or remove a key from an existing
json object, by identifying it using a [json path](https://datatracker.ietf.org/doc/rfc9535/) expression.

For instance, given an existing array like this:

{% raw %}

```handlebars
{{#assign 'existingArray'}}
[
{ "id": 456, "name": "bob"},
{ "id": 123, "name": "alice"},
{ "id": 321, "name": "sam"}
]
{{/assign}}
```

{% endraw %}

application of this helper, which selects the object with id `123`:

{% raw %}

```handlebars
{{jsonRemove existingArray '$.[?(@.id == 123)]'}}
```

{% endraw %}

will return this array:

{% raw %}

```json
[
{ "id": 456, "name": "bob"},
{ "id": 321, "name": "sam"}
]
```

{% endraw %}

Given an object like this:

{% raw %}

```handlebars
{{#assign 'existingObject'}}
{ "id": 456, "name": "bob"}
{{/assign}}
```

{% endraw %}

application of this helper, which selects the key name:

{% raw %}

```handlebars
{{jsonRemove existingObject '$.name'}}
```

{% endraw %}

will return this object:

{% raw %}

```json
{ "id": 456 }
```

{% endraw %}

## Date and time helpers

A helper is present to render the current date/time, with the ability to specify the format ([via Java's SimpleDateFormat](https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html)) and offset.
Expand Down

0 comments on commit ff44bed

Please sign in to comment.