JSON 数据操作

Exisi 2023-01-19 12:51:05
Categories: Tags:

 

  1. 自动验证存储在 JSON 列中的 JSON 文档。无效的文档会产生错误。

 

  1. 优化的存储格式。存储在 JSON 列中的 JSON 文档被转换为允许对文档元素进行快速读取访问的内部格式。

 

  1. 当服务器稍后必须读取以这种二进制格式存储的 JSON 值时,不需要从文本表示中解析该值。

 

  1. 二进制格式的结构使服务器能够直接通过键值对或数组索引查找子对象或嵌套值,而无需读取文档中它们之前或之后的所有值。

 

 

 

 

 

JSON 值的创建

 

["abc", 10, null, true, false]

 

 

{"k1": "value", "k2": 10}

 

 

["12:18:29.000000", "2015-07-29", "2015-07-29 12:18:29.000000"]

 

 

[99, {"id": "HK500", "cost": 75.99}, ["hot", "cold"]] {"k1": "value", "k2": [10, 20]}

示例

CREATE TABLE tab_base_info (

id BIGINT NOT NULL PRIMARY KEY auto_increment,

content json

);

 

 

 

JSON 值的插入

 

示例

NSERT INTO tab_base_info (content)

VALUES

(

'{"author": "Captain&D", "blog": "https://www.cnblogs.com/captainad"}'

);

 

 

 

JSON 值的部分更新

 

  1. 正在更新的列被声明为 JSON

 

  1. UPDATE 语句使用 JSON_SET()JSON_REPLACE() JSON_REMOVE() 三个函数中的任何一个来更新列。列值的直接分配(例如,UPDATE mytable SET jcol = '{"a": 10, "b": 25}')不能作为部分更新执行。

 

  1. 可以以这种方式优化单个 UPDATE 语句中多个 JSON 列的更新; MySQL 只能对那些使用刚刚列出的三个函数更新其值的列执行部分更新。

 

  1. 输入列和目标列必须是同一列; UPDATE mytable SET jcol1 = JSON_SET(jcol2, '$.a', 100) 之类的语句不能作为部分更新执行。只要输入列和目标列相同,更新就可以任意组合使用对上一项中列出的任何函数的嵌套调用。

 

  1. 所有更改都将现有数组或对象值替换为新值,并且不会向父对象或数组添加任何新元素。

 

  1. 被替换的值必须至少与替换值一样大。换句话说,新值不能大于旧值。

 

 

 

JSON 值的规范化

 

mysql> SELECT JSON_OBJECT('key1', 1, 'key2', 'abc', 'key1', 'def');

+-------------------------------------------------------------+

|JSON_OBJECT('key1', 1, 'key2', 'abc', 'key1', 'def') |

+-------------------------------------------------------------+

|{"key1": "def", "key2": "abc"}                                        |

+-------------------------------------------------------------+

 

 

mysql> CREATE TABLE t1 (c1 JSON);

mysql> INSERT INTO t1 VALUES

> ('{"x": 17, "x": "red"}'),

> ('{"x": 17, "x": "red", "x": [3, 5, 7]}');

mysql> SELECT c1 FROM t1;

+------------------+

|c1                      |

+------------------+

|{"x": "red"}        |

|{"x": [3, 5, 7]}    |

+------------------+

 

 

mysql> SELECT JSON_OBJECT('key1', 1, 'key2', 'abc', 'key1', 'def');

+-------------------------------------------------------------+

|JSON_OBJECT('key1', 1, 'key2', 'abc', 'key1', 'def') |

+-------------------------------------------------------------+

|{"key1": 1, "key2": "abc"}                                               |

+-------------------------------------------------------------+

 




JSON 值的合并

 

 

 

 

 

 

您可以比较此查询显示的结果:

 

mysql> SELECT     

-> JSON_MERGE_PRESERVE('[1, 2]', '["a", "b", "c"]', '[true, false]') AS Preserve,     

-> JSON_MERGE_PATCH('[1, 2]', '["a", "b", "c"]', '[true, false]') AS Patch\G

***************************1. row ***************************

Preserve:[1, 2, "a", "b", "c", true, false]
   Patch
:[true, false]

 

 

以下查询说明了重复密钥a的结果差异:

 

mysql> SELECT    

 -> JSON_MERGE_PRESERVE('{"a": 1, "b": 2}', '{"c": 3, "a": 4}', '{"c": 5, "d": 3}') AS Preserve,     

-> JSON_MERGE_PATCH('{"a": 3, "b": 2}', '{"c": 3, "a": 4}', '{"c": 5, "d": 3}') AS Patch\G

***************************1. row ***************************

Preserve:{"a":[1, 4], "b":2, "c":[3, 5], "d":3}
   Patch
:{"a":4, "b":2, "c":5, "d":3}

 

 

如下所示:

mysql> SELECT          

-> JSON_MERGE_PRESERVE('1', '2') AS Preserve,          

-> JSON_MERGE_PATCH('1', '2') AS Patch\G

***************************1. row ***************************

Preserve:[1, 2]
   Patch
:2

 

 

如本例所示:

mysql> SELECT          

-> JSON_MERGE_PRESERVE('[10, 20]', '{"a": "x", "b": "y"}') AS Preserve,          

-> JSON_MERGE_PATCH('[10, 20]', '{"a": "x", "b": "y"}') AS Patch\G

***************************1. row ***************************

Preserve:[10, 20, {"a":"x", "b":"y"}]
   Patch
:{"a":"x", "b":"y"}

 

 

 

JSON 值的搜索和修改

 

 

mysql> SELECT JSON_EXTRACT('{"id": 14, "name": "Aztalan"}', '$.name');

+------------------------------------------------------------------+

|JSON_EXTRACT('{"id": 14, "name": "Aztalan"}', '$.name') |

+------------------------------------------------------------------+

|"Aztalan"                                                                              |

+------------------------------------------------------------------+

 

 

 

mysql> SELECT JSON_SET('"x"', '$[0]', 'a');

+----------------------------------+

|JSON_SET('"x"', '$[0]', 'a')  |

+----------------------------------+

|"a"                                            |

+----------------------------------+

1 row in set (0.00 sec)

 

 

last作为最右边数组元素索引的同义词受支持。还支持数组元素的相对寻址。如果path未选择数组值,则path[last]的计算结果与path相同,如本节后面所示(请参阅最右边的数组元素)。

 

路径可以包含***通配符:

 

 

[3, {"a": [5, 6], "b": 10}, [99, 100]]

 

 

 

{"a fish": "shark", "a bird": "sparrow"}

 

两个键都包含空格,并且必须引用:

 

 

 

mysql> SELECT JSON_EXTRACT('{"a": 1, "b": 2, "c": [3, 4, 5]}', '$.*');

+-------------------------------------------------------------+

|JSON_EXTRACT('{"a": 1, "b": 2, "c": [3, 4, 5]}', '$.*') |

+-------------------------------------------------------------+

|[1, 2, [3, 4, 5]]                                                               |

+-------------------------------------------------------------+

mysql> SELECT JSON_EXTRACT('{"a": 1, "b": 2, "c": [3, 4, 5]}', '$.c[*]');

+----------------------------------------------------------------+

|JSON_EXTRACT('{"a": 1, "b": 2, "c": [3, 4, 5]}', '$.c[*]') |

+----------------------------------------------------------------+

|[3, 4, 5]                                                                             |

+----------------------------------------------------------------+

 

 

mysql> SELECT JSON_EXTRACT('{"a": {"b": 1}, "c": {"b": 2}}', '$**.b');

+---------------------------------------------------------------+

|JSON_EXTRACT('{"a": {"b": 1}, "c": {"b": 2}}', '$**.b')   |

+---------------------------------------------------------------+

|[1, 2]                                                                                |

+---------------------------------------------------------------+

 

 

mysql> SELECT JSON_EXTRACT('[1, 2, 3, 4, 5]', '$[1 to 3]');

+----------------------------------------------------+

|JSON_EXTRACT('[1, 2, 3, 4, 5]', '$[1 to 3]')  |

+----------------------------------------------------+

|[2, 3, 4]                                                            |

+----------------------------------------------------+

1 row in set (0.00 sec)

 

 

 

mysql> SELECT JSON_EXTRACT('[1, 2, 3, 4, 5]', '$[last-3 to last-1]'); +--------------------------------------------------------------+

|JSON_EXTRACT('[1, 2, 3, 4, 5]', '$[last-3 to last-1]') | +--------------------------------------------------------------+

|[2, 3, 4]                                                                          |

+--------------------------------------------------------------+

1 row in set (0.01 sec)

 

mysql> SELECT JSON_REPLACE('"Sakila"', '$[last]', 10);

+-----------------------------------------------+

|JSON_REPLACE('"Sakila"', '$[last]', 10) |

+-----------------------------------------------+

|10                                                              |

+-----------------------------------------------+

1 row in set (0.00 sec)

 

 

 

 

 ​​​​​​​

来自 <https://dev.mysql.com/doc/refman/8.0/en/json.html>