SQL JSON_TYPE function
JSON_TYPE(JSON_EXTRACT(doc, '$.tags')) tells you whether a value is an array, object, string or number, which predicts whether JSON_VALUE or JSON_QUERY will return it.
- Categoria
- Strings
- Returns
text- In this category
- 143 of 172
Sintaxe
JSON_TYPE(json_val) -- MySQL
json_type(json [, path]) -- SQLite
jsonb_typeof(jsonb) -- PostgreSQL
Each block is a separate form. The support table and the examples show which engine takes which.
Suporte a mecanismos
| Motor | Support | Notas |
|---|---|---|
| SQL Server | Not built in | no JSON_TYPE; OPENJSON's type column (0-5), or ISJSON(x, ARRAY) on 2022+ |
| PostgreSQL | Different name or syntax | jsonb_typeof; also json_typeof for the json type |
| MySQL | Sim | Supported under this name. |
| SQLite | Sim | Supported under this name. |
| Oracle | Not built in | the type() item method: JSON_VALUE(doc, '$.tags.type()') |
Exemplos
MySQL
SELECT JSON_TYPE(config) AS doc_type,
JSON_TYPE(config->'$.tags') AS tags_type
FROM settings;
SQLite
SELECT json_type(payload, '$.count') FROM events;
PostgreSQL
SELECT jsonb_typeof(data->'count') FROM payloads;
Bom saber
- The vocabularies differ. MySQL returns upper case: OBJECT, ARRAY, STRING, INTEGER, DOUBLE, DECIMAL, BOOLEAN, NULL. SQLite returns object, array, text, integer, real, true, false, null. PostgreSQL returns object, array, string, number, boolean, null.
- MySQL's result has a binary collation, so JSON_TYPE(x) = 'array' is false for an array; compare with 'ARRAY'.
- MySQL's JSON_TYPE takes one argument: JSON_TYPE(doc, '$.tags') is a parameter-count error, so extract first. SQLite accepts a path; PostgreSQL navigates with -> and then calls jsonb_typeof.
Questions about JSON_TYPE
How do I check if a path holds an array?
MySQL: JSON_TYPE(doc->'$.tags') = 'ARRAY'. SQLite: json_type(doc, '$.tags') = 'array'. PostgreSQL: jsonb_typeof(data->'tags') = 'array'. Oracle: JSON_VALUE(doc, '$.tags.type()') = 'array'.
Does JSON_TYPE return NULL for JSON null?
No. A JSON null reports as 'null' (MySQL: 'NULL'). SQL NULL comes back when the input is SQL NULL or, on SQLite, when the path does not exist.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
Cheat sheets for the engines that have it: PostgreSQL, MySQL, SQLite.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.