0

I had a column in PostgreSQL which has text datatype (data text)

the data in this column will be like

[
 {"code":"ABC","Value":"A1"},
 {"code":"Name","Value":"Ramesh"},
 {"code":"Age","paramValue":"24"}
]

I need to add new parameter to it such as

{"code":"Mobile","paramValue":"9XXXXXXXXX"}

the data would be like

[
 {"code":"ABC","Value":"A1"},
 {"code":"Name","Value":"Ramesh"},
 {"code":"Age","paramValue":"24"},
 {"code":"Mobile","paramValue":"9XXXXXXXXX"}
]

I tried many ways but I couldn't succeed can i get suggestion on this

4
  • Welcome to SO. Could you also add these attempts you mentioned to the question? Commented May 27, 2021 at 14:23
  • am sorry i am new PG first i tried reading json like SELECT id, data ::json->'"code":"ABC"' as name FROM my_table; i am getting error ERROR: invalid input syntax for type json DETAIL: Expected "," or "}", but found ":". Commented May 27, 2021 at 14:34
  • means you want to add an object in JSON array Commented May 27, 2021 at 14:36
  • yes, i need an object Commented May 27, 2021 at 14:37

1 Answer 1

1

Simply use || operator. Documentation

Considering your structure is always a valid JSON Array stored in text column.

Try This:

create table test (id int, json_field text);

insert into test values(1,'[{"code":"ABC","Value":"A1"},
{"code":"Name","Value":"Ramesh"},
{"code":"Age","paramValue":"24"}]');

Final Code

update test set json_field=
json_field::jsonb || '{"mobile":"kXXXXXXXX"}'
where id=1

DEMO

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

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.