How to check if string is valid json in template

hi
im trying to find a way in template code to check if a string is valid json
checked chagpt but it gave me false answers and non supported syntax
if I do this code

{% set json_string = 'invalid' %}
{% set json_data = json_string | from_json  is not none %}

in HA dev templates tab, i get a json decode error and the template rendering is stopped (i.e. if its inside automation or lovelace card)

"{{ value_json is defined }}"

what is value_json? a function ? how does it get the json string iput to parse ?
i want to check if a string variable json_string has a valid json content

It is an example.

value_json is a variable returned by many integrations, e.g. mqtt, restful, etc…

Substitute it for whatever is holding your json data if not using those integrations.

its not an integration
its something i get from some external system as string
is there a way to validate it in template code?
i asked GPT and it says i need to write python script to do this, impossible via template

How?

Don’t do that. It is trained on very out of date data and likes to confidently bullshit you.

No it isn’t. Use the is defined test.

i get it via some API from my mobile phone
but lets for sake of question assume its just some input text sensor that the user types the value
and i want to validate the value in automation tobe valid json string
is it supported?

For the last time, how?

Which integration are you using to interrogate the API?

Any string in jinja is a valid string in json. So your question isn’t making much sense. You need to provide more information.

ill try to clarify…

forget about where data is from,

This is a general question if HA template can validate a string to be JSON and give either True or False result. This can be done in python / Java code with ease, I want to know if possible via templates

lets say i have this code in dev tools templates

{% set my_string = 'garbage string'   %}
{% set valid_json_obj = ( my_string | from_json ) %}
{{ valid_json_obj  is not none }}

this gives json decode error and if run via automation, it will stops rendering the automation
in dev tab you get this error

JSONDecodeError: unexpected character: line 1 column 1 (char 0)

but i want instead for it to end with False for above case and I can decide in my templates code what to do after

is it possible or not?

That is not possible.

ok, this is the answer i was looking for, thank you,

and BTW chatgpt also said that in the end after suggesting many things that do not work …

seems shame, as it its supported by python which is the HA engine to run templates among other things

It’s not supported by python, you do a try except in python. There is no function in orjson or json that ignores exceptions. And jinja does not support try except.

yes , python/Java with try/except that was i referring to

thanks

Ideally from_json would have a default argument so that when it encounters a string it cannot parse it reports the supplied default value as opposed to throwing an error.

For example, the output of this would be none.

{{ 'gobbledygook' | from_json(none) }}

The alternative is it simply defaults to none (or unknown) if it fails to parse the input string.

If it interests you, I suggest you create a Feature Request for it.

3 Likes

wow, thanks, i guess it will be incorporated in one of comting versions of HA uopdates?

Looks like it has been approved. Probably too late for 2025.6 (currently in beta testing) but should be in 2025.7.