Collections:
JSON_TYPE() - Detecting Type of JSON Value
How to detect the type of a JSON (JavaScript Object Notation) value using the JSON_TYPE() function?
✍: FYIcenter.com
JSON_TYPE(json) is a MySQL built-in function that
returns the data type of a JSON (JavaScript Object Notation) value.
For example:
SELECT JSON_TYPE('[]'), JSON_TYPE('{}');
-- +-----------------+-----------------+
-- | JSON_TYPE('[]') | JSON_TYPE('{}') |
-- +-----------------+-----------------+
-- | ARRAY | OBJECT |
-- +-----------------+-----------------+
SELECT JSON_TYPE('null'), JSON_TYPE('true');
-- +-------------------+-------------------+
-- | JSON_TYPE('null') | JSON_TYPE('true') |
-- +-------------------+-------------------+
-- | NULL | BOOLEAN |
-- +-------------------+-------------------+
SELECT JSON_TYPE('"FYI"'), JSON_TYPE('999');
-- +--------------------+------------------+
-- | JSON_TYPE('"FYI"') | JSON_TYPE('999') |
-- +--------------------+------------------+
-- | STRING | INTEGER |
-- +--------------------+------------------+
Note that JSON_TYPE() does not return the official type NUMERIC on JSON numeric scalars. It actually returns the MySQL data type of any JSON numeric scalar. For example:
SELECT JSON_TYPE('999'), JSON_TYPE('9.99');
-- +------------------+-------------------+
-- | JSON_TYPE('999') | JSON_TYPE('9.99') |
-- +------------------+-------------------+
-- | INTEGER | DOUBLE |
-- +------------------+-------------------+
Also note that the argument must be a valid JSON encoded MySQL string. Other MySQL values will result an error.
SELECT JSON_TYPE(999); ERROR 3146 (22032): Invalid data type for JSON data in argument 1 to function json_type; a JSON string or JSON type is required.
Reference information of the JSON_TYPE() function:
JSON_TYPE(json): int Returns the data type of a JSON (JavaScript Object Notation) value. Arguments, return value and availability: json: Required. The JSON value to be observed. int: Return value. The data type of the JSON value. Available since MySQL 5.7.
⇒ JSON_UNQUOTE() - Unquoting JSON String
⇐ JSON_TABLE() - Inline Table with JSON Array
2025-03-12, 2126🔥, 0💬
Popular Posts:
What Privilege Is Needed for a User to Delete Rows from Tables in Another Schema in Oracle? For a us...
How To Provide Default Values to Function Parameters in SQL Server Transact-SQL? If you add a parame...
How Fixed Length Strings Are Truncated and Padded in SQL Server Transact-SQL? When the length of the...
How To Create a Table Index in Oracle? If you have a table with a lots of rows, and you know that on...
How To List All Stored Procedures in the Current Database in SQL Server Transact-SQL? If you want to...