0

I have a table T with a column J of type json, and one of it's row contains value

{'SELECTION':'A'}


I want to change it to

{'selection':'A'}

i.e change the key of the JSON.

Any one has experience? I am not able to find any relevant resource.

2

2 Answers 2

0

Thanks to @Sebastian, I solved it using the documentation https://www.postgresql.org/docs/current/functions-json.html

update T set J = '{"selection":"A"}' where  J::jsonb @> '{"SELECTION":"A"}'::jsonb

Another query that will do same

update T set J = '{"selection":"A"}' where J::json->>'SELECTION'='A'
Sign up to request clarification or add additional context in comments.

Comments

0

For me this worked well.

create table form
(
  id integer, 
  data json
);

insert into form (id, data) 
values (1000, 
    $$
      {"formType": "DEFAULT", "old_key": {"column": [{"old_key": "1"}, {"old_key": "7"}, {"old_key": "22"}]}}  
    $$::json    
  )
;

SELECT *
FROM form;

UPDATE form
set data = data::jsonb - 'old_key' || jsonb_build_object('new_key', data->'old_key')
where data::jsonb ? 'old_key';

SELECT *
FROM form;

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.