mirror of
https://github.com/home-assistant/home-assistant.io.git
synced 2025-07-24 09:46:59 +00:00
Add-default-to-from-json (#39405)
This commit is contained in:
parent
0af34ac2e0
commit
29272d9cc0
@ -903,6 +903,29 @@ The temperature is 25°C
|
|||||||
|
|
||||||
{% endraw %}
|
{% 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
|
### 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.
|
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.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user