Available in versions: Dev (3.22) | Latest (3.21) | 3.20 | 3.19 | 3.18

JSON_SET

Supported by ✅ Open Source Edition   ✅ Express Edition   ✅ Professional Edition   ✅ Enterprise Edition

The MySQL style JSON_SET function is a function that adds (like JSON_INSERT) or replaces (JSON_REPLACE) a value in a JSON document, given a JSON path.

SELECT
  JSON_SET('{"a":1}', '$.a', 2),
  JSON_SET('{"a":1}', '$.b', 2)
create.select(
           jsonSet(val(json("{\"a\":1}")), "$.a", 2),
           jsonSet(val(json("{\"a\":1}")), "$.b", 2)).fetch();

The result would look like this:

+----------+---------------+
| json_set | json_set      |
+----------+---------------+
| {"a":2}  | {"a":1,"b":2} |
+----------+---------------+

Dialect support

This example using jOOQ:

jsonSet(val(json("{\"a\":1}")), "$.a", 2)

Translates to the following dialect specific expressions:

BigQuery

json_set('{"a":1}', '$.a', 2)

MariaDB 10.2+

json_set(json_extract('{"a":1}', '$'), '$.a', 2)

MySQL 5.7+

json_set(cast('{"a":1}' AS json), '$.a', 2)

Oracle 23ai+

json_transform('{"a":1}', SET '$.a' = 2)

Oracle 21c

json_transform('{"a":1}', SET '$.a' = nvl(NULL, 2))

SQLServer 2016+

json_modify(
  json_modify('{"a":1}', '$.a', ''),
  ('strict ' + '$.a'),
  2
)

SQLite

json_set(JSON('{"a":1}'), '$.a', 2)

ASE, Access, Aurora MySQL, Aurora Postgres, ClickHouse, CockroachDB, DB2, Databricks, DuckDB, Exasol, Firebird, H2, HSQLDB, Hana, Informix, MariaDB 10.0 to 10.1, MemSQL, MySQL 5.6, Oracle 18c to 20c, Postgres, Redshift, SQLDataWarehouse, SQLServer 2012 to 2014, Snowflake, Sybase, Teradata, Trino, Vertica, YugabyteDB

/* UNSUPPORTED */
Generated with jOOQ 3.20. Support in older jOOQ versions may differ. Translate your own SQL on our website

Feedback

Do you have any feedback about this page? We'd love to hear it!

The jOOQ Logo