1
The scenario is as follows, I have a JSONB type column in my database where a JSON structure is saved.
What I need is that when this structure is updated, it will update only the keys that have been requested to update and maintain the existing ones. If you give a common update on this type of column it just replaces the old data with new ones, however I need to perform a MERGE.
In some other code scenarios I have used the following structure:
update tabela set coluna=(coluna || (jsonDeEntrada)::jsonb);
It works for the cases I had until today, but my new JSON structure has more or less this organization:
{
"key1": {
"key1.1": {
"key": "valor",
"key": "valor"
},
"key1.2": {}
},
"key2": {
"key2.1": {
"key": "valor",
"key": "valor"
},
"key2.2": {}
},
"key3": {
"key": "valor",
"key": "valor",
"key": "valor"
}
}
In certain scenarios i only need to change one of the keys inside the key1.1
, then I would only need update its value and keep the others, but what is done (even using that MERGE structure I put up there) is that it replaces all the contents of key1.1
by the new JSON passed, in this case deleting the other keys that were not mentioned in the UPDATE.
There just happens to be some effective way to merge directly into Postgresql, or will I really need to always create the object in the same structure and include the data I don’t want to change as well, to replace the entire JSON?