// strings reference

SQL JSON_EXISTS

JSON_EXISTS(doc, '$.a') is true when the path is present, even when its value is JSON null. That is the difference from testing JSON_VALUE for NULL.

Kategorie
Strings
Returns
boolean
In this category
167 of 172

Syntax

JSON_EXISTS(doc, path)

Engine-Unterstützung

EngineSupportNotizen
SQL Server Different name or syntax JSON_PATH_EXISTS(doc, path), 2022+; JSON_EXISTS is not recognized
PostgreSQL Ja JSON_EXISTS from 17; jsonb_path_exists and the ? operator before
MySQL Different name or syntax JSON_CONTAINS_PATH(doc, 'one', path)
SQLite Different name or syntax json_type(doc, path) IS NOT NULL
Oracle Ja JSON_EXISTS

Beispiele

PostgreSQL

SELECT JSON_EXISTS('{"a":null}'::jsonb, '$.a') AS null_key, JSON_EXISTS('{"a":1}'::jsonb, '$.b') AS missing;  -- t, f

SQL Server

SELECT JSON_PATH_EXISTS('{"a":null}', '$.a') AS null_key, JSON_PATH_EXISTS('{"a":1}', '$.b') AS missing;  -- 1, 0

MySQL

SELECT JSON_CONTAINS_PATH('{"a":null}', 'one', '$.a') AS null_key, JSON_CONTAINS_PATH('{"a":1}', 'one', '$.b') AS missing;  -- 1, 0

Oracle

SELECT CASE WHEN JSON_EXISTS('{"a":1}', '$.b') THEN 'y' ELSE 'n' END AS has_b FROM dual;  -- n

Gut zu wissen

  • A key whose value is JSON null exists: JSON_EXISTS on '{"a":null}' and '$.a' is true. JSON_VALUE(doc, '$.a') IS NOT NULL treats the same key as missing.
  • Before 23ai, Oracle's JSON_EXISTS is a condition for WHERE and CASE WHEN. 23ai's BOOLEAN type also lets you select it.
  • PostgreSQL and Oracle accept filter expressions in the path: '$.items[*] ? (@.qty > 5)' is true when any item has qty over 5.

Questions about JSON_EXISTS

How do I filter rows that contain a key?

WHERE JSON_EXISTS(doc, '$.region') on PostgreSQL 17+ and Oracle, WHERE JSON_PATH_EXISTS(doc, '$.region') = 1 on SQL Server, WHERE JSON_CONTAINS_PATH(doc, 'one', '$.region') on MySQL, WHERE doc ? 'region' on PostgreSQL jsonb.

Can JSON_EXISTS test a value, not just a path?

With a filter, yes: JSON_EXISTS(doc, '$.items[*] ? (@.qty > 5)') on PostgreSQL and Oracle. MySQL's JSON_CONTAINS tests for a value instead.

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite, Oracle.

// fewer lookups

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.