coding-practicing

mysql json

JOSON_ARRAY

JSON_LENGTH() 函数

返回 JSON 文档或者 JSON 文档中通过路径指定的节点的长度

JSON_LENGTH(json)
JSON_LENGTH(json, path)

如果指定了 path, JSON_LENGTH() 函数返回 JSON 文档中由路径指定的值的长度,否则返回 JSON 文档的长度。 JSON_LENGTH() 函数按照如下规则计算 JSON 文档的长度:

如果存在以下的情况,JSON_LENGTH() 函数将返回 NULL:

JSON_LENGTH() 函数将在以下情况下返回错误:

参考

exam

SELECT
    JSON_LENGTH('1') as `1`,
    JSON_LENGTH('true') as `true`,
    JSON_LENGTH('false') as `false`,
    JSON_LENGTH('null') as `null`,
    JSON_LENGTH('"abc"') as `"abc"`;

-- +------+------+-------+------+-------+
-- | 1    | true | false | null | "abc" |
-- +------+------+-------+------+-------+
-- |    1 |    1 |     1 |    1 |     1 |
-- +------+------+-------+------+-------+

-- 数组
SELECT
    JSON_LENGTH('[]') as `[]`,
    JSON_LENGTH('[1, 2]') as `[1, 2]`,
    JSON_LENGTH('[1, {"x": 2}]') as `[1, {"x": 2}]`;

-- +------+--------+---------------+
-- | []   | [1, 2] | [1, {"x": 2}] |
-- +------+--------+---------------+
-- |    0 |      2 |             2 |
-- +------+--------+---------------+

-- 对象
SELECT
    JSON_LENGTH('{}') as `[]`,
    JSON_LENGTH('{"x": 1, "y": 2}') as `{"x": 1, "y": 2}`,
    JSON_LENGTH('{"x": 1, "y": {"z" : 2}}') as `{"x": 1, "y": {"z" : 2}}`;

-- +------+------------------+--------------------------+
-- | []   | {"x": 1, "y": 2} | {"x": 1, "y": {"z" : 2}} |
-- +------+------------------+--------------------------+
-- |    0 |                2 |                        2 |
-- +------+------------------+--------------------------+

-- 路径
SELECT JSON_LENGTH('{"x": 1, "y": [1, 2]}', '$.y');

-- 这里, 路径表达式 $.y 对应的值是 [1, 2],[1, 2] 的长度为 2。
-- +---------------------------------------------+
-- | JSON_LENGTH('{"x": 1, "y": [1, 2]}', '$.y') |
-- +---------------------------------------------+
-- |                                           2 |
-- +---------------------------------------------+

-- 这相当先使用 JSON_EXTRACT() 函数提取路径匹配的部分,再计算长度,如下:
SELECT JSON_LENGTH(JSON_EXTRACT('{"x": 1, "y": [1, 2]}', '$.y'));
-- +-----------------------------------------------------------+
-- | JSON_LENGTH(JSON_EXTRACT('{"x": 1, "y": [1, 2]}', '$.y')) |
-- +-----------------------------------------------------------+
-- |                                                         2 |
-- +-----------------------------------------------------------+

JSON_SEARCH函数

返回一个给定字符串在一个 JSON 文档中的路径(它返回一个路径字符串或者由多个路径组成的数组。)

语法

JSON_SEARCH(json, one_or_all, search_str)
JSON_SEARCH(json, one_or_all, search_str, escape_char)
JSON_SEARCH(json, one_or_all, search_str, escape_char, path)

example

SET @json = '[
  {
    "name": "Tim",
    "age": 20,
    "hobbies": [
      { "name": "Car", "weight": 10 },
      { "name": "Sports", "weight": 20 }
    ]
  },
  {
    "name": "Tom",
    "age": 20,
    "hobbies": [
      { "name": "Reading", "weight": 10 },
      { "name": "Sports", "weight": 20 }
    ]
  }
]';

-- 搜索字符串
SELECT JSON_SEARCH(@json, 'one', 'Tim');
-- +----------------------------------+
-- | JSON_SEARCH(@json, 'one', 'Tim') |
-- +----------------------------------+
-- | "$[0].name"                      |
-- +----------------------------------+

-- one all
SELECT
    JSON_SEARCH(@json, 'one', 'Sports'),
    JSON_SEARCH(@json, 'all', 'Sports');
-- +-------------------------------------+--------------------------------------------------+
-- | JSON_SEARCH(@json, 'one', 'Sports') | JSON_SEARCH(@json, 'all', 'Sports')              |
-- +-------------------------------------+--------------------------------------------------+
-- | "$[0].hobbies[1].name"              | ["$[0].hobbies[1].name", "$[1].hobbies[1].name"] |
-- +-------------------------------------+--------------------------------------------------+

-- 通配符
SELECT JSON_SEARCH(@json, 'all', 'S%');

-- +--------------------------------------------------+
-- | JSON_SEARCH(@json, 'all', 'S%')                  |
-- +--------------------------------------------------+
-- | ["$[0].hobbies[1].name", "$[1].hobbies[1].name"] |
-- +--------------------------------------------------+