Updating a JSON field
suggest changeIn the previous example we saw how mixed data types can be inserted into a JSON field. What if we want to update that field? We are going to add scheveningen to the array named variations in the previous example.
UPDATE
myjson
SET
dict=JSON_ARRAY_APPEND(dict,'$.variations','scheveningen')
WHERE
id = 2;
Notes:
$.variationsis an array in the JSON document.$is the document root. For the path syntax, see https://dev.mysql.com/doc/refman/5.7/en/json-path-syntax.html- Since we don’t yet have an example on querying using json fields, this example uses the primary key.
Now if we do SELECT * FROM myjson we will see
+----+-----------------------------------------------------------------------------------------+
| id | dict |
+---+-----------------------------------------------------------------------------------------+
| 2 | {"opening": "Sicilian", "variations": ["pelikan", "dragon", "najdorf", "scheveningen"]} |
+----+-----------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents