JSON_REPLACE() – Replace Values in a JSON Document in MySQL

In MySQL, the JSON_REPLACE() function replaces values in a JSON document and returns the result.

You provide the JSON document as the first argument, followed by the path to replace data at, followed with the value to replace the existing value with.

You can replace data at multiple places in the document if required. To do this, simply provide multiple path/value pairs as required.

Continue reading

JSON_MERGE_PATCH() vs JSON_MERGE_PRESERVE() in MySQL: What’s the Difference?

MySQL includes a number of functions for working with JSON documents. Among these are the JSON_MERGE_PATCH() and JSON_MERGE_PRESERVE() functions.

Both of these functions merge two or more JSON documents and return the result. However, there are certain cases where these functions will return a different result. You need to be aware of this before including them in any of your queries.

Continue reading

JSON_ARRAY_APPEND() – Append Values to a JSON Array in MySQL

When using JSON documents with MySQL, we can use the JSON_ARRAY_APPEND() function to append new values to an array.

The way it works is, you provide the JSON document as the first argument, then follow that up with the path to append to, followed by the value to append.

In MySQL 5.7, this function was called JSON_APPEND() but that name is no longer supported.

Continue reading

JSON_QUOTE() – How to Escape Characters in Strings used as JSON Values in MySQL

When working with JSON and MySQL, sometimes you might need to escape strings so that their quote characters don’t interfere with the interpretation of the JSON document. In such cases, you can use the JSON_QUOTE() function to escape potentially problematic strings.

JSON_QUOTE() is used to produce a valid JSON string literal that can be included in a JSON document.  For example, you might want an element in an array to have the text null as a string literal, rather than having an actual null value. This function can be used to ensure that the text is added as a string literal, rather than a null value.

To use it, simply call the function while passing in the string.

Continue reading