While SQL 2016 added support for JSON, I don't think it specifies way to merge or update JSON (only create and query). This means you will need to use DB-specific features, which requires knowing what your database is.
Postgresql has the ||
operator which will do the job directly:
# select '{"abc":"value1", "xyz":"value2"}'::jsonb || '{"pqr":"value3"}'::jsonb;
?column?
-----------------------------------------------------
{"abc": "value1", "pqr": "value3", "xyz": "value2"}
(1 row)
In other databases, you'll probably have to compose with lower-level utilities (json_set
/json_modify
) and manual manipulation.
SQLite also has json_patch
which would do the job here, however it's a much more powerful and flexible utility based around RFC-7396 JSON Patching, so if the "patcher" is user-controlled it's probably not a good idea.