2

Hi I have a json column in Oracle database with a data like [{"id":100, "make":"BMW"},{"id":110,"make":"mercedes"}]..... now how can I update the make of object with id 110 to Toyota using sql/plsql..Thank you..

3
  • 1
    Please post what have you tried so far? Commented Dec 22, 2019 at 15:00
  • Does this answer your question? How to update JSON column in oracle 12.1 Commented Dec 22, 2019 at 20:22
  • You can do it with SQL function json_transform , which is available in the Oracle Database 20c preview and will be in the 21c release. Commented Aug 20, 2020 at 21:54

3 Answers 3

0

You can use json_table() function for version 12.1.0.2+ to parse a json column. Btw, a string type column such as clob, varchar2 might be checked out by adding a check constraint for concerned string type column. So use :

update tab
   set jsdata=(select case when js.id = 110 then replace(jsdata,js.make,'toyota') end
                 from tab
                cross join
                      json_table(jsdata, '$[*]'
                         columns(make    varchar2(50) path '$.make',
                                 id      int path '$.id')) js
                where js.id = 110   ) 

Demo

Sign up to request clarification or add additional context in comments.

Comments

0

Unfortunately, it is not easy to change data within an array.

create table t(
  id number primary key,
  json_ds varchar2(4000) check(json_ds is json)
);
insert into t values (1, '[{"id":100, "make":"BMW"},{"id":110,"make":"mercedes"}]');
commit;

update /*+ WITH_PLSQL */ t a
set json_ds = (
  with function update_json(p_in in varchar2) return varchar2 is
    l_ja json_array_t;
    l_po json_object_t;
    l_id number;
  begin
    l_ja := json_array_t.parse(p_in);
    for i in 0..l_ja.get_size - 1 loop
      l_po := json_object_t(l_ja.get(i));
      l_id := l_po.get_number('id');
      if l_id = 110 then
        l_po.put('make', 'Toyota');
      end if;
    end loop;
    return l_ja.to_string;
  end update_json;
  select update_json(a.json_ds) from dual
)
where id = 1;
/
select * from t;

ID  JSON_DS
1   [{"id":100,"make":"BMW"},{"id":110,"make":"Toyota"}]

Best regards, Stew Ashton

Comments

0

You can use JSON_TRANSFORM() in 19.8 and up.

with example as
 (select '[{"id":100, "make":"BMW"},{"id":110,"make":"mercedes"}]' as json from dual)
select json_transform(example.json, set '$[*]?(@.id==110).make' = 'Toyota') as newjson from example;

Output:

[{"id":100,"make":"BMW"},{"id":110,"make":"Toyota"}]

You can also use JSON_MERGEPATCH (19c and up) but it can't update within arrays, need to update the whole array

Regards

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.