mysql> SELECT JSON_PRETTY('123'); # scalar+--------------------+| JSON_PRETTY('123') |+--------------------+| 123 |+--------------------+mysql> SELECT JSON_PRETTY("[1,3,5]"); # array+------------------------+| JSON_PRETTY("[1,3,5]") |+------------------------+| [1,3,5] |+------------------------+mysql> SELECT JSON_PRETTY('{"a":"10","b":"15","x":"25"}'); # object+---------------------------------------------+| JSON_PRETTY('{"a":"10","b":"15","x":"25"}') |+---------------------------------------------+| {"a": "10","b": "15","x": "25"} |+---------------------------------------------+mysql> SELECT JSON_PRETTY('["a",1,{"key1":'> "value1"},"5", "77" ,'> {"key2":["value3","valueX",'> "valueY"]},"j", "2" ]')\G # nested arrays and objects*************************** 1. row ***************************JSON_PRETTY('["a",1,{"key1":"value1"},"5", "77" ,{"key2":["value3","valuex","valuey"]},"j", "2" ]'): ["a",1,{"key1": "value1"},"5","77",{"key2": ["value3","valuex","valuey"]},"j","2"]
mysql> CREATE TABLE jtable (jcol JSON);Query OK, 0 rows affected (0.38 sec)mysql> INSERT INTO jtable VALUES-> ('{"a": 10, "b": "wxyz", "c": "[true, false]"}');Query OK, 1 row affected (0.04 sec)mysql> SELECT * FROM jtable;+----------------------------------------------+| jcol |+----------------------------------------------+| {"a": 10, "b": "wxyz", "c": "[true, false]"} |+----------------------------------------------+1 row in set (0.00 sec)
mysql> UPDATE jtable-> SET jcol = JSON_SET(jcol, "$.a", 10, "$.b", "wxyz", "$.c", 1);Query OK, 1 row affected (0.03 sec)Rows matched: 1 Changed: 1 Warnings: 0mysql> SELECT * FROM jtable;+--------------------------------+| jcol |+--------------------------------+| {"a": 10, "b": "wxyz", "c": 1} |+--------------------------------+1 row in set (0.00 sec)mysql> SELECT JSON_STORAGE_FREE(jcol) FROM jtable;+-------------------------+| JSON_STORAGE_FREE(jcol) |+-------------------------+| 14 |+-------------------------+1 row in set (0.00 sec)
mysql> UPDATE jtable-> SET jcol = JSON_SET(jcol, "$.a", 10, "$.b", "wx", "$.c", 1);Query OK, 1 row affected (0.03 sec)Rows matched: 1 Changed: 1 Warnings: 0mysql> SELECT JSON_STORAGE_FREE(jcol) FROM jtable;+-------------------------+| JSON_STORAGE_FREE(jcol) |+-------------------------+| 16 |+-------------------------+1 row in set (0.00 sec)
mysql> UPDATE jtable SET jcol = '{"a": 10, "b": 1}';Query OK, 1 row affected (0.05 sec)Rows matched: 1 Changed: 1 Warnings: 0mysql> SELECT JSON_STORAGE_FREE(jcol) FROM jtable;+-------------------------+| JSON_STORAGE_FREE(jcol) |+-------------------------+| 0 |+-------------------------+1 row in set (0.00 sec)
mysql> SET @j = '{"a": 10, "b": "wxyz", "c": "[true, false]"}';Query OK, 0 rows affected (0.00 sec)mysql> SET @j = JSON_SET(@j, '$.a', 10, '$.b', 'wxyz', '$.c', '1');Query OK, 0 rows affected (0.00 sec)mysql> SELECT @j, JSON_STORAGE_FREE(@j) AS Free;+----------------------------------+------+| @j | Free |+----------------------------------+------+| {"a": 10, "b": "wxyz", "c": "1"} | 0 |+----------------------------------+------+1 row in set (0.00 sec)
mysql> SELECT JSON_STORAGE_FREE('{"a": 10, "b": "wxyz", "c": "1"}') AS Free;+------+| Free |+------+| 0 |+------+1 row in set (0.00 sec)
mysql> CREATE TABLE jtable (jcol JSON);Query OK, 0 rows affected (0.42 sec)mysql> INSERT INTO jtable VALUES-> ('{"a": 1000, "b": "wxyz", "c": "[1, 3, 5, 7]"}');Query OK, 1 row affected (0.04 sec)mysql> SELECT-> jcol,-> JSON_STORAGE_SIZE(jcol) AS Size,-> JSON_STORAGE_FREE(jcol) AS Free-> FROM jtable;+-----------------------------------------------+------+------+| jcol | Size | Free |+-----------------------------------------------+------+------+| {"a": 1000, "b": "wxyz", "c": "[1, 3, 5, 7]"} | 47 | 0 |+-----------------------------------------------+------+------+1 row in set (0.00 sec)
mysql> UPDATE jtable SET jcol =-> JSON_SET(jcol, "$.b", "a");Query OK, 1 row affected (0.04 sec)Rows matched: 1 Changed: 1 Warnings: 0mysql> SELECT-> jcol,-> JSON_STORAGE_SIZE(jcol) AS Size,-> JSON_STORAGE_FREE(jcol) AS Free-> FROM jtable;+--------------------------------------------+------+------+| jcol | Size | Free |+--------------------------------------------+------+------+| {"a": 1000, "b": "a", "c": "[1, 3, 5, 7]"} | 47 | 3 |+--------------------------------------------+------+------+1 row in set (0.00 sec)
mysql> UPDATE jtablemysql> SET jcol = '{"a": 4.55, "b": "wxyz", "c": "[true, false]"}';Query OK, 1 row affected (0.04 sec)Rows matched: 1 Changed: 1 Warnings: 0mysql> SELECT-> jcol,-> JSON_STORAGE_SIZE(jcol) AS Size,-> JSON_STORAGE_FREE(jcol) AS Free-> FROM jtable;+------------------------------------------------+------+------+| jcol | Size | Free |+------------------------------------------------+------+------+| {"a": 4.55, "b": "wxyz", "c": "[true, false]"} | 56 | 0 |+------------------------------------------------+------+------+1 row in set (0.00 sec)
mysql> SET @j = '[100, "sakila", [1, 3, 5], 425.05]';Query OK, 0 rows affected (0.00 sec)mysql> SELECT @j, JSON_STORAGE_SIZE(@j) AS Size;+------------------------------------+------+| @j | Size |+------------------------------------+------+| [100, "sakila", [1, 3, 5], 425.05] | 45 |+------------------------------------+------+1 row in set (0.00 sec)mysql> SET @j = JSON_SET(@j, '$[1]', "json");Query OK, 0 rows affected (0.00 sec)mysql> SELECT @j, JSON_STORAGE_SIZE(@j) AS Size;+----------------------------------+------+| @j | Size |+----------------------------------+------+| [100, "json", [1, 3, 5], 425.05] | 43 |+----------------------------------+------+1 row in set (0.00 sec)mysql> SET @j = JSON_SET(@j, '$[2][0]', JSON_ARRAY(10, 20, 30));Query OK, 0 rows affected (0.00 sec)mysql> SELECT @j, JSON_STORAGE_SIZE(@j) AS Size;+---------------------------------------------+------+| @j | Size |+---------------------------------------------+------+| [100, "json", [[10, 20, 30], 3, 5], 425.05] | 56 |+---------------------------------------------+------+1 row in set (0.00 sec)
mysql> SELECT-> JSON_STORAGE_SIZE('[100, "sakila", [1, 3, 5], 425.05]') AS A,-> JSON_STORAGE_SIZE('{"a": 1000, "b": "a", "c": "[1, 3, 5, 7]"}') AS B,-> JSON_STORAGE_SIZE('{"a": 1000, "b": "wxyz", "c": "[1, 3, 5, 7]"}') AS C,-> JSON_STORAGE_SIZE('[100, "json", [[10, 20, 30], 3, 5], 425.05]') AS D;+----+----+----+----+| A | B | C | D |+----+----+----+----+| 45 | 44 | 47 | 56 |+----+----+----+----+1 row in set (0.00 sec)
文章转载自林员外聊编程,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




