Outdated Version

You are viewing an older version of this section. View current production version.

JSON_SET_<type>

This is a set of three JSON functions, which sets the value of a given key in a JSON map or array.

Maps are in the form:

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

And arrays are in the form:

[1, 2, 3]
  • JSON_SET_DOUBLE is for all numeric data. Javascript has only one numeric type.
  • JSON_SET_STRING is for all text and binary types.
  • JSON_SET_JSON is for all valid Javascript types, including true, false, JSON maps, and lists.
JSON_SET_<type>(json, keyname, value)

Arguments

  • json: a valid JSON map or array, or the name of a JSON column.
  • keyname: the named key to set value to, or the zero-indexed array position.
  • value: the value to set

If the keyname does not exist in a map, it will be added. If it does exist, the value will be overwritten. If an array index is past the end of the array, intervening values will be set to NULL.

Return Type

  • The complete map or array
  • SQL NULL if json is not a valid JSON map or array

Examples

mysql> select json_set_double('{"a":1, "b":2}', 'z', 26) as set_z;
+----------------------+
| set_z                |
+----------------------+
| {"a":1,"b":2,"z":26} |
+----------------------+

mysql> select json_set_double('[1,2,3]', 4, 26) as set_4;
+-----------------+
| set_4           |
+-----------------+
| [1,2,3,null,26] |
+-----------------+
Info

A JSON or Javascript null value is distinct from SQL NULL.