Sling Academy
Home/MySQL/MySQL 8: Using JSON_SET() function to insert or update a JSON document

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

Last updated: January 26, 2024

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.

Next Article: How to Prevent SQL Injection in MySQL 8: Explained with Examples

Previous Article: Making use of the JSON_REMOVE() function in MySQL 8

Series: MySQL Tutorials: From Basic to Advanced

MySQL

You May Also Like

  • MySQL: How to reset the AUTO_INCREMENT value of a table
  • MySQL: How to add a calculated column to SELECT query
  • MySQL: Eliminate orphan rows in one-to-many relationships
  • MySQL: Using R-Tree Indexes for Spatial Data Types
  • How to Create Custom Collations in MySQL
  • Using Hash Indexes in MySQL: A Practical Guide
  • Understanding Full-Text Indexes in MySQL
  • Partial Indexes in MySQL: A Practical Guide
  • MySQL: How to Remove FOREIGN KEY Constraints
  • Using ENUM in MySQL 8: A Practical Guide (with Examples)
  • MySQL: Creating a Fixed-Size Table by Using Triggers
  • One-to-Many Relationship in MySQL 8: A Practical Guide
  • Using Regular Expressions in MySQL 8: The Complete Guide
  • Using Loops in MySQL: A Practical Guide (with Examples)
  • How to Execute an SQL File in VS Code
  • Making use of the JSON_REMOVE() function in MySQL 8
  • MySQL 8: How to count rows in related tables
  • Replication in MySQL 8: A Comprehensive Guide
  • MySQL 8: FIRST_VALUE(), LAST_VALUE(), and NTH_VALUE() functions – Explained with examples