MySQL 8: Using JSON_SET() function to insert or update a JSON document

Updated: January 26, 2024 By: Guest Contributor Post a comment

Introduction

MySQL 8 introduced a suite of JSON functions that provide powerful capabilities for working with JSON documents. Among these, the JSON_SET() function stands out as an essential tool for anyone dealing with JSON data types in MySQL databases. It allows for inserting or updating values in a JSON document efficiently. This guide walks you through the basics of the JSON_SET() function and progresses to more advanced applications with practical code examples.

The Basic of JSON_SET()

The JSON_SET() function in MySQL is used to update the value of a JSON document or insert a value if it does not exist. The syntax is as follows:

JSON_SET(json_doc, path, value[, path, value] ...)

Where json_doc is the original JSON document, and path is the location within the document where the new value should be set. If the path does not exist, it will be added to the JSON document.

Basic Usage of JSON_SET()

Let’s start with a simple example:

SET @json = '{"a": 1, "b": 2}';
SELECT JSON_SET(@json, '$.c', 3);

The output of this will be a JSON document with the added key `c`:

{"a": 1, "b": 2, "c": 3}

Updating Values

If we want to update the value of a key:

SET @json = '{"a": 1, "b": 2}';
SELECT JSON_SET(@json, '$.b', 'updated');

After the update, the JSON document will be:

{"a": 1, "b": "updated"}

Inserting Nested JSON Objects

Here’s how you can insert a nested JSON object:

SET @json = '{"a": 1}';
SELECT JSON_SET(@json, '$.b', JSON_OBJECT('c', 2));

The resulting JSON document will have a new nested object:

{"a": 1, "b": {"c": 2}}

Working with JSON Arrays

Updating an Array Element

How to update a value within a JSON array:

SET @json = '{"array": [1, 2, 3]}';
SELECT JSON_SET(@json, '$.array[1]', 4);

The updated JSON document will have `4` as the second element in the array:

{"array": [1, 4, 3]}

Inserting a New Element to an Array

To insert a new element to the array:

SET @json = '{"array": [1, 2, 3]}';
SELECT JSON_SET(@json, '$.array[3]', 4);

This will append the number `4` to the JSON array:

{"array": [1, 2, 3, 4]}

Advanced Use Cases

Handling Multiple Updates

Here’s how JSON_SET() can be used to update multiple paths within a JSON document at once:

SET @json = '{"a": 1, "b": 2}';
SELECT JSON_SET(@json, '$.a', 'newA', '$.b', 'newB');

The result will be:

{"a": "newA", "b": "newB"}

Using JSON_SET() with MySQL Tables

Consider a MySQL table `users` with a JSON column `preferences` that we’d like to update:

UPDATE users
SET preferences = JSON_SET(preferences, '$.theme', 'dark')
WHERE id = 1;

This will change the `theme` preference to `’dark’` for the user with ID 1.

Conclusion

The JSON_SET() function is a flexible tool for manipulating JSON documents in MySQL. Whether you’re working with simple objects or complex hierarchies, JSON_SET() makes it straightforward to insert and update data in JSON format. With practice, it becomes a valuable part of any developer’s MySQL toolkit.