JSON
Applies to: ✅ Data 360 SQL ✅ Tableau Hyper API
Parses a string and interprets it as a JSON value, validating that the string is well-formed JSON.
Syntax
1json(<string> [FORMAT JSON])
You can also parse a string into JSON with a cast.
1<string>::json
2CAST(<string> AS json)
3TRY_CAST(<string> AS json)
Arguments
Required
<string>: The string to parse as JSON. The string must be a valid JSON document.
Optional
FORMAT JSON: Accepted for SQL-standard compatibility. It has no effect on the result.
Returns
Returns a json value. Use the result to embed pre-formatted JSON into json_array or json_object, or wherever a json value is expected.
Considerations
- The input string is validated as JSON. Invalid input is rejected with an
invalid JSON document error.
- The cast forms
<string>::json and CAST(<string> AS json) perform the same validation as the json constructor.
- Use
TRY_CAST(<string> AS json) if you want to suppress validation errors.
- The
WITH UNIQUE KEYS option from the SQL-standard, which would reject objects with duplicate keys, isn’t supported.
- Parsing preserves the document’s textual content, including insignificant whitespace.
Examples
Parse a JSON String
Interpret a string as a JSON value.
1SELECT json('{"a":1}') AS result;
Returns {"a":1}.
Parse with a Cast
A cast from a string performs the same validation.
1SELECT '[]'::json AS result;
Returns [].
Embed Pre-Formatted JSON
Use the constructor to embed an existing document without escaping it.
1SELECT json_object('a': 1, 'b': json('{"nested":true}')) AS result;
Returns {"a":1,"b":{"nested":true}}.
Invalid JSON Is Rejected
Malformed input raises an error.
1SELECT json('{"a": invalid}');
Raises an invalid JSON document error.
Related Documentation
- JSON Type
[JSON_ARRAY](./json-array.md) - Build a JSON array from a list of values.
[JSON_OBJECT](./json-object.md) - Build a JSON object from key-value pairs.
[JSON_SCALAR](./json-scalar.md) - Convert a scalar value to JSON.
- Type Conversions - Type casting and coercion.