2

Am trying to update a JSON column in oracle 12.1. Is there any json function that can be used to update the JSON. I have added a constraint JSON_COLUMN is JSON.

I have tried :

UPDATE tablename SET JSON =
json_mergepatch(JSON, '{"STRUCTURE_NAME":null}');

But this function is applicable only for 19c

This "STRUCTURE_NAME": "ABC:STATE:L12345", needs to be updated with "STRUCTURE_NAME":null

2 Answers 2

3

Pre-19c, if you want to change any values in a JSON document, you have to replace the whole thing:

create table t (
  doc varchar2(100)
    check ( doc is json ) 
);

insert into t values ('{
  "changeMe" : "to null",
  "leaveMe"  : "alone"
}');

update t
set    doc = '{
  "changeMe" : null,
  "leaveMe"  : "alone"
}';

select * from t;

DOC                                               
{
  "changeMe" : null,
  "leaveMe"  : "alone"
}  

Note that when you get to 19c and use json_mergepatch, setting an attribute to null removes it from the document:

update t
set    doc = json_mergepatch ( 
  doc, 
  '{
    "changeMe" : null
  }');

select * from t;

DOC                   
{"leaveMe":"alone"}   
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks Chris - The JSON which am trying to update is around 5MB , sorting in a JSON format would result in 53K lines . I would like to update just a parameter : Updated JSON:[SOURCE_SYSTEM] been updated to null: "Type": "DROP", "SOURCE_SYSTEM": "NULL", "TYPE": "ABCD" Can we update just the parameter , rather than using the whole JSON: UPDATE tbl SET DOCUMENT_COLUMN = REPLACE(JSON, SOURCE_SYSTEM, 'NULL');
JSON is just text, so in theory yes. You have to be sure there's nothing else in the document matching the replace though!
JSON: "node": [{ "Type": "DROP", "Links": "", "VERSION_ID": 12345678, "ID": "12345678", "CAB_ID": "650252419", "AEND": "ABC_123456", "BEND": "650252418", "SEQUENCE_NO": 0, "STATUS": "SUCCESS", "DEL_FLAG": "2019-03-25T19:36:26Z", "CUR_WO_ID": 123456, "CREATED_DT": "2018-12-19T16:21:54Z", "MODIFIED_DT": "2019-02-06T14:23:00Z", "SOURCE_SYSTEM": "NULL", "TYPE": "ABCD" {] UPDATE tablename SET JSON = REPLACE(JSON, 'node.STATUS', 'REVERTED') ;This hasn't helped. It has shown 1 row updated , but hasn't got updated.
That's because there's no string node.STATUS in your JSON! You can't use JSON path expressions in replace. You need to search for the exact text in the document you want to change. e.g. if in your example you want "TYPE": "ABCD" to become null, you need to REPLACE(JSON, '"TYPE": "ABCD"', '"TYPE": null'). Or just REPLACE(JSON, '"ABCD"', 'null') and hope "ABCD" is nowhere else in the document!
Yes , It has worked . Any way to update with the JSON path expression in 12.1
|
0

Below query will overwrite the JSON document column for all rows.

UPDATE JSON_TABLE
SET JSON_DOC_COL=JSON_OBJECT('name' VALUE var_name,
                             'age' VALUE var_age);

To append the JSON document column, below query can be used.

UPDATE JSON_TABLE
SET JSON_DOC_COL=JSON_MERGEPATCH(JSON_DOC_COL,JSON_OBJECT('name' VALUE var_name,
                             'age' VALUE var_age) RETURNING CLOB);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.