Add-default-to-from-json (#39405)

This commit is contained in:
Petro31 2025-06-24 11:03:35 -04:00 committed by GitHub
parent 0af34ac2e0
commit 29272d9cc0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -903,6 +903,29 @@ The temperature is 25°C
{% endraw %}
`from_json(default)` function will attempt to convert the input to `json`. If that fails, returns the `default` value, or if omitted raises an error.
#### Template
{% raw %}
```text
{% set result = 'not json'|from_json('not json') %}
The value is {{ result }}
```
{% endraw %}
#### Output
{% raw %}
```text
The value is not json
```
{% endraw %}
### Is defined
Sometimes a template should only return if a value or object is defined, if not, the supplied default value should be returned. This can be useful to validate a JSON payload.