IS_VALID_JSON function - Amazon Redshift

IS_VALID_JSON function

The IS_VALID_JSON function validates a JSON string. The function returns Boolean true (t) if the string is properly formed JSON or false (f) if the string is malformed. To validate a JSON array, use IS_VALID_JSON_ARRAY function

For more information, see JSON functions.

Syntax

is_valid_json('json_string')

Arguments

json_string

A string or expression that evaluates to a JSON string.

Return type

BOOLEAN

Example

The following example creates a table and inserts JSON strings for testing.

create table test_json(id int identity(0,1), json_strings varchar); -- Insert valid JSON strings -- insert into test_json(json_strings) values ('{"a":2}'), ('{"a":{"b":{"c":1}}}'), ('{"a": [1,2,"b"]}'); -- Insert invalid JSON strings -- insert into test_json(json_strings)values ('{{}}'), ('{1:"a"}'), ('[1,2,3]');

The following example validates the strings in the preceding example.

select id, json_strings, is_valid_json(json_strings) from test_json order by id; id | json_strings | is_valid_json ---+---------------------+-------------- 0 | {"a":2} | true 2 | {"a":{"b":{"c":1}}} | true 4 | {"a": [1,2,"b"]} | true 6 | {{}} | false 8 | {1:"a"} | false 10 | [1,2,3] | false