From 29272d9cc0bd4cd8abbd14f9b36728ccdc57afe4 Mon Sep 17 00:00:00 2001 From: Petro31 <35082313+Petro31@users.noreply.github.com> Date: Tue, 24 Jun 2025 11:03:35 -0400 Subject: [PATCH] Add-default-to-from-json (#39405) --- .../_docs/configuration/templating.markdown | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/source/_docs/configuration/templating.markdown b/source/_docs/configuration/templating.markdown index 3a481425f52..48f8ec087a8 100644 --- a/source/_docs/configuration/templating.markdown +++ b/source/_docs/configuration/templating.markdown @@ -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.