Rechercher dans le manuel MySQL

12.17.8 JSON Utility Functions

This section documents utility functions that act on JSON values, or strings that can be parsed as JSON values. JSON_PRETTY() prints out a JSON value in a format that is easy to read. JSON_STORAGE_SIZE() and JSON_STORAGE_FREE() show, respectively, the amount of storage space used by a given JSON value and the amount of space remaining in a JSON column following a partial update.

  • JSON_PRETTY(json_val)

    Provides pretty-printing of JSON values similar to that implemented in PHP and by other languages and database systems. The value supplied must be a JSON value or a valid string representation of a JSON value. Extraneous whitespaces and newlines present in this value have no effect on the output. For a NULL value, the function returns NULL. If the value is not a JSON document, or if it cannot be parsed as one, the function fails with an error.

    Formatting of the output from this function adheres to the following rules:

    • Each array element or object member appears on a separate line, indented by one additional level as compared to its parent.

    • Each level of indentation adds two leading spaces.

    • A comma separating individual array elements or object members is printed before the newline that separates the two elements or members.

    • The key and the value of an object member are separated by a colon followed by a space (': ').

    • An empty object or array is printed on a single line. No space is printed between the opening and closing brace.

    • Special characters in string scalars and key names are escaped employing the same rules used by the JSON_QUOTE() function.

    1. mysql> SELECT JSON_PRETTY('123'); # scalar
    2. +--------------------+
    3. | JSON_PRETTY('123') |
    4. +--------------------+
    5. | 123                |
    6. +--------------------+
    7.  
    8. mysql> SELECT JSON_PRETTY("[1,3,5]"); # array
    9. +------------------------+
    10. | JSON_PRETTY("[1,3,5]") |
    11. +------------------------+
    12. | [
    13.   1,
    14.   3,
    15.   5
    16. ]      |
    17. +------------------------+
    18.  
    19. mysql> SELECT JSON_PRETTY('{"a":"10","b":"15","x":"25"}'); # object
    20. +---------------------------------------------+
    21. | JSON_PRETTY('{"a":"10","b":"15","x":"25"}') |
    22. +---------------------------------------------+
    23. | {
    24.   "a": "10",
    25.   "b": "15",
    26.   "x": "25"
    27. }   |
    28. +---------------------------------------------+
    29.  
    30. mysql> SELECT JSON_PRETTY('["a",1,{"key1":
    31.    '>    "value1"},"5",     "77" ,
    32.     '>       {"key2":["value3","valueX",
    33.    '> "valueY"]},"j", "2"   ]')\G  # nested arrays and objects
    34. *************************** 1. row ***************************
    35. JSON_PRETTY('["a",1,{"key1":
    36.              "value1"},"5",     "77" ,
    37.                 {"key2":["value3","valuex",
    38.           "valuey"]},"j", "2"   ]'): [
    39.  "a",
    40.  1,
    41.  {
    42.    "key1": "value1"
    43.  },
    44.  "5",
    45.  "77",
    46.  {
    47.    "key2": [
    48.      "value3",
    49.      "valuex",
    50.      "valuey"
    51.    ]
    52.  },
    53.  "j",
    54.  "2"
    55. ]
  • JSON_STORAGE_FREE(json_val)

    For a JSON column value, this function shows how much storage space was freed in its binary representation after it was updated in place using JSON_SET(), JSON_REPLACE(), or JSON_REMOVE(). The argument can also be a valid JSON document or a string which can be parsed as one—either as a literal value or as the value of a user variable—in which case the function returns 0. It returns a positive, nonzero value if the argument is a JSON column value which has been updated as described previously, such that its binary representation takes up less space than it did prior to the update. For a JSON column which has been updated such that its binary representation is the same as or larger than before, or if the update was not able to take advantage of a partial update, it returns 0; it returns NULL if the argument is NULL.

    If json_val is not NULL, and neither is a valid JSON document nor can be successfully parsed as one, an error results.

    In this example, we create a table containing a JSON column, then insert a row containing a JSON object:

    1. mysql> CREATE TABLE jtable (jcol JSON);
    2. Query OK, 0 rows affected (0.38 sec)
    3.  
    4. mysql> INSERT INTO jtable VALUES
    5.     ->     ('{"a": 10, "b": "wxyz", "c": "[true, false]"}');
    6. Query OK, 1 row affected (0.04 sec)
    7.  
    8. mysql> SELECT * FROM jtable;
    9. +----------------------------------------------+
    10. | jcol                                         |
    11. +----------------------------------------------+
    12. | {"a": 10, "b": "wxyz", "c": "[true, false]"} |
    13. +----------------------------------------------+
    14. 1 row in set (0.00 sec)

    Now we update the column value using JSON_SET() such that a partial update can be performed; in this case, we replace the value pointed to by the c key (the array [true, false]) with one that takes up less space (the integer 1):

    1. mysql> UPDATE jtable
    2.     ->     SET jcol = JSON_SET(jcol, "$.a", 10, "$.b", "wxyz", "$.c", 1);
    3. Query OK, 1 row affected (0.03 sec)
    4. Rows matched: 1  Changed: 1  Warnings: 0
    5.  
    6. mysql> SELECT * FROM jtable;
    7. +--------------------------------+
    8. | jcol                           |
    9. +--------------------------------+
    10. | {"a": 10, "b": "wxyz", "c": 1} |
    11. +--------------------------------+
    12. 1 row in set (0.00 sec)
    13.  
    14. mysql> SELECT JSON_STORAGE_FREE(jcol) FROM jtable;
    15. +-------------------------+
    16. | JSON_STORAGE_FREE(jcol) |
    17. +-------------------------+
    18. |                      14 |
    19. +-------------------------+
    20. 1 row in set (0.00 sec)

    The effects of successive partial updates on this free space are cumulative, as shown in this example using JSON_SET() to reduce the space taken up by the value having key b (and making no other changes):

    1. mysql> UPDATE jtable
    2.     ->     SET jcol = JSON_SET(jcol, "$.a", 10, "$.b", "wx", "$.c", 1);
    3. Query OK, 1 row affected (0.03 sec)
    4. Rows matched: 1  Changed: 1  Warnings: 0
    5.  
    6. mysql> SELECT JSON_STORAGE_FREE(jcol) FROM jtable;
    7. +-------------------------+
    8. | JSON_STORAGE_FREE(jcol) |
    9. +-------------------------+
    10. |                      16 |
    11. +-------------------------+
    12. 1 row in set (0.00 sec)

    Updating the column without using JSON_SET(), JSON_REPLACE(), or JSON_REMOVE() means that the optimizer cannot perform the update in place; in this case, JSON_STORAGE_FREE() returns 0, as shown here:

    1. mysql> UPDATE jtable SET jcol = '{"a": 10, "b": 1}';
    2. Query OK, 1 row affected (0.05 sec)
    3. Rows matched: 1  Changed: 1  Warnings: 0
    4.  
    5. mysql> SELECT JSON_STORAGE_FREE(jcol) FROM jtable;
    6. +-------------------------+
    7. | JSON_STORAGE_FREE(jcol) |
    8. +-------------------------+
    9. |                       0 |
    10. +-------------------------+
    11. 1 row in set (0.00 sec)

    Partial updates of JSON documents can be performed only on column values. For a user variable that stores a JSON value, the value is always completely replaced, even when the update is performed using JSON_SET():

    1. mysql> SET @j = '{"a": 10, "b": "wxyz", "c": "[true, false]"}';
    2. Query OK, 0 rows affected (0.00 sec)
    3.  
    4. mysql> SET @j = JSON_SET(@j, '$.a', 10, '$.b', 'wxyz', '$.c', '1');
    5. Query OK, 0 rows affected (0.00 sec)
    6.  
    7. mysql> SELECT @j, JSON_STORAGE_FREE(@j) AS Free;
    8. +----------------------------------+------+
    9. | @j                               | Free |
    10. +----------------------------------+------+
    11. | {"a": 10, "b": "wxyz", "c": "1"} |    0 |
    12. +----------------------------------+------+
    13. 1 row in set (0.00 sec)

    For a JSON literal, this function always returns 0:

    1. mysql> SELECT JSON_STORAGE_FREE('{"a": 10, "b": "wxyz", "c": "1"}') AS Free;
    2. +------+
    3. | Free |
    4. +------+
    5. |    0 |
    6. +------+
    7. 1 row in set (0.00 sec)
  • JSON_STORAGE_SIZE(json_val)

    This function returns the number of bytes used to store the binary representation of a JSON document. When the argument is a JSON column, this is the space used to store the JSON document as it was inserted into the column, prior to any partial updates that may have been performed on it afterwards. json_val must be a valid JSON document or a string which can be parsed as one. In the case where it is string, the function returns the amount of storage space in the JSON binary representation that is created by parsing the string as JSON and converting it to binary. It returns NULL if the argument is NULL.

    An error results when json_val is not NULL, and is not—or cannot be successfully parsed as—a JSON document.

    To illustrate this function's behavior when used with a JSON column as its argument, we create a table named jtable containing a JSON column jcol, insert a JSON value into the table, then obtain the storage space used by this column with JSON_STORAGE_SIZE(), as shown here:

    1. mysql> CREATE TABLE jtable (jcol JSON);
    2. Query OK, 0 rows affected (0.42 sec)
    3.  
    4. mysql> INSERT INTO jtable VALUES
    5.     ->     ('{"a": 1000, "b": "wxyz", "c": "[1, 3, 5, 7]"}');
    6. Query OK, 1 row affected (0.04 sec)
    7.  
    8. mysql> SELECT
    9.     ->     jcol,
    10.     ->     JSON_STORAGE_SIZE(jcol) AS Size,
    11.     ->     JSON_STORAGE_FREE(jcol) AS Free
    12.     -> FROM jtable;
    13. +-----------------------------------------------+------+------+
    14. | jcol                                          | Size | Free |
    15. +-----------------------------------------------+------+------+
    16. | {"a": 1000, "b": "wxyz", "c": "[1, 3, 5, 7]"} |   47 |    0 |
    17. +-----------------------------------------------+------+------+
    18. 1 row in set (0.00 sec)

    According to the output of JSON_STORAGE_SIZE(), the JSON document inserted into the column takes up 47 bytes. We also checked the amount of space freed by any previous partial updates of the column using JSON_STORAGE_FREE(); since no updates have yet been performed, this is 0, as expected.

    Next we perform an UPDATE on the table that should result in a partial update of the document stored in jcol, and then test the result as shown here:

    1. mysql> UPDATE jtable SET jcol =
    2.     ->     JSON_SET(jcol, "$.b", "a");
    3. Query OK, 1 row affected (0.04 sec)
    4. Rows matched: 1  Changed: 1  Warnings: 0
    5.  
    6. mysql> SELECT
    7.     ->     jcol,
    8.     ->     JSON_STORAGE_SIZE(jcol) AS Size,
    9.     ->     JSON_STORAGE_FREE(jcol) AS Free
    10.     -> FROM jtable;
    11. +--------------------------------------------+------+------+
    12. | jcol                                       | Size | Free |
    13. +--------------------------------------------+------+------+
    14. | {"a": 1000, "b": "a", "c": "[1, 3, 5, 7]"} |   47 |    3 |
    15. +--------------------------------------------+------+------+
    16. 1 row in set (0.00 sec)

    The value returned by JSON_STORAGE_FREE() in the previous query indicates that a partial update of the JSON document was performed, and that this freed 3 bytes of space used to store it. The result returned by JSON_STORAGE_SIZE() is unchanged by the partial update.

    Partial updates are supported for updates using JSON_SET(), JSON_REPLACE(), or JSON_REMOVE(). The direct assignment of a value to a JSON column cannot be partially updated; following such an update, JSON_STORAGE_SIZE() always shows the storage used for the newly-set value:

    1. mysql> UPDATE jtable
    2. mysql>     SET jcol = '{"a": 4.55, "b": "wxyz", "c": "[true, false]"}';
    3. Query OK, 1 row affected (0.04 sec)
    4. Rows matched: 1  Changed: 1  Warnings: 0
    5.  
    6. mysql> SELECT
    7.     ->     jcol,
    8.     ->     JSON_STORAGE_SIZE(jcol) AS Size,
    9.     ->     JSON_STORAGE_FREE(jcol) AS Free
    10.     -> FROM jtable;
    11. +------------------------------------------------+------+------+
    12. | jcol                                           | Size | Free |
    13. +------------------------------------------------+------+------+
    14. | {"a": 4.55, "b": "wxyz", "c": "[true, false]"} |   56 |    0 |
    15. +------------------------------------------------+------+------+
    16. 1 row in set (0.00 sec)

    A JSON user variable cannot be partially updated. This means that this function always shows the space currently used to store a JSON document in a user variable:

    1. mysql> SET @j = '[100, "sakila", [1, 3, 5], 425.05]';
    2. Query OK, 0 rows affected (0.00 sec)
    3.  
    4. mysql> SELECT @j, JSON_STORAGE_SIZE(@j) AS Size;
    5. +------------------------------------+------+
    6. | @j                                 | Size |
    7. +------------------------------------+------+
    8. | [100, "sakila", [1, 3, 5], 425.05] |   45 |
    9. +------------------------------------+------+
    10. 1 row in set (0.00 sec)
    11.  
    12. mysql> SET @j = JSON_SET(@j, '$[1]', "json");
    13. Query OK, 0 rows affected (0.00 sec)
    14.  
    15. mysql> SELECT @j, JSON_STORAGE_SIZE(@j) AS Size;
    16. +----------------------------------+------+
    17. | @j                               | Size |
    18. +----------------------------------+------+
    19. | [100, "json", [1, 3, 5], 425.05] |   43 |
    20. +----------------------------------+------+
    21. 1 row in set (0.00 sec)
    22.  
    23. mysql> SET @j = JSON_SET(@j, '$[2][0]', JSON_ARRAY(10, 20, 30));
    24. Query OK, 0 rows affected (0.00 sec)
    25.  
    26. mysql> SELECT @j, JSON_STORAGE_SIZE(@j) AS Size;
    27. +---------------------------------------------+------+
    28. | @j                                          | Size |
    29. +---------------------------------------------+------+
    30. | [100, "json", [[10, 20, 30], 3, 5], 425.05] |   56 |
    31. +---------------------------------------------+------+
    32. 1 row in set (0.00 sec)

    For a JSON literal, this function always returns the current storage space used:

    1. mysql> SELECT
    2.     ->     JSON_STORAGE_SIZE('[100, "sakila", [1, 3, 5], 425.05]') AS A,
    3.     ->     JSON_STORAGE_SIZE('{"a": 1000, "b": "a", "c": "[1, 3, 5, 7]"}') AS B,
    4.     ->     JSON_STORAGE_SIZE('{"a": 1000, "b": "wxyz", "c": "[1, 3, 5, 7]"}') AS C,
    5.     ->     JSON_STORAGE_SIZE('[100, "json", [[10, 20, 30], 3, 5], 425.05]') AS D;
    6. +----+----+----+----+
    7. | A  | B  | C  | D  |
    8. +----+----+----+----+
    9. | 45 | 44 | 47 | 56 |
    10. +----+----+----+----+
    11. 1 row in set (0.00 sec)

Zoek in de MySQL-handleiding

Nederlandse vertaling

U hebt gevraagd om deze site in het Nederlands te bezoeken. Voor nu wordt alleen de interface vertaald, maar nog niet alle inhoud.

Als je me wilt helpen met vertalingen, is je bijdrage welkom. Het enige dat u hoeft te doen, is u op de site registreren en mij een bericht sturen waarin u wordt gevraagd om u toe te voegen aan de groep vertalers, zodat u de gewenste pagina's kunt vertalen. Een link onderaan elke vertaalde pagina geeft aan dat u de vertaler bent en heeft een link naar uw profiel.

Bij voorbaat dank.

Document heeft de 26/06/2006 gemaakt, de laatste keer de 26/10/2018 gewijzigd
Bron van het afgedrukte document:https://www.gaudry.be/nl/mysql-rf-json-utility-functions.html

De infobrol is een persoonlijke site waarvan de inhoud uitsluitend mijn verantwoordelijkheid is. De tekst is beschikbaar onder CreativeCommons-licentie (BY-NC-SA). Meer info op de gebruiksvoorwaarden en de auteur.

Referenties

  1. Bekijk - html-document Taal van het document:en Manuel MySQL : https://dev.mysql.com/

Deze verwijzingen en links verwijzen naar documenten die geraadpleegd zijn tijdens het schrijven van deze pagina, of die aanvullende informatie kunnen geven, maar de auteurs van deze bronnen kunnen niet verantwoordelijk worden gehouden voor de inhoud van deze pagina.
De auteur Deze site is als enige verantwoordelijk voor de manier waarop de verschillende concepten, en de vrijheden die met de referentiewerken worden genomen, hier worden gepresenteerd. Vergeet niet dat u meerdere broninformatie moet doorgeven om het risico op fouten te verkleinen.

Inhoudsopgave Haut