json_contains

description

Syntax

BOOLEAN json_contains(JSON json_str, JSON candidate) BOOLEAN json_contains(JSON json_str, JSON candidate, VARCHAR json_path) BOOLEAN json_contains(VARCHAR json_str, VARCHAR candidate, VARCHAR json_path)

Indicates by returning 1 or 0 whether a given candidate JSON document is contained at a specific path within the json_str JSON document

example

  1. mysql> SET @j = '{"a": 1, "b": 2, "c": {"d": 4}}';
  2. mysql> SET @j2 = '1';
  3. mysql> SELECT JSON_CONTAINS(@j, @j2, '$.a');
  4. +-------------------------------+
  5. | JSON_CONTAINS(@j, @j2, '$.a') |
  6. +-------------------------------+
  7. | 1 |
  8. +-------------------------------+
  9. mysql> SELECT JSON_CONTAINS(@j, @j2, '$.b');
  10. +-------------------------------+
  11. | JSON_CONTAINS(@j, @j2, '$.b') |
  12. +-------------------------------+
  13. | 0 |
  14. +-------------------------------+
  15. mysql> SET @j2 = '{"d": 4}';
  16. mysql> SELECT JSON_CONTAINS(@j, @j2, '$.a');
  17. +-------------------------------+
  18. | JSON_CONTAINS(@j, @j2, '$.a') |
  19. +-------------------------------+
  20. | 0 |
  21. +-------------------------------+
  22. mysql> SELECT JSON_CONTAINS(@j, @j2, '$.c');
  23. +-------------------------------+
  24. | JSON_CONTAINS(@j, @j2, '$.c') |
  25. +-------------------------------+
  26. | 1 |
  27. +-------------------------------+
  28. mysql> SELECT json_contains('[1, 2, {"x": 3}]', '1');
  29. +----------------------------------------+
  30. | json_contains('[1, 2, {"x": 3}]', '1') |
  31. +----------------------------------------+
  32. | 1 |
  33. +----------------------------------------+
  34. 1 row in set (0.04 sec)

keywords

json,json_contains