Jezen Thomas
Jezen Thomas

Reputation: 13800

How do I update a JSON field in PostgreSQL?

I have a table with a column called data that contains some JSON. If the data column for any given row in the table is not null, it will contain a JSON-encoded object with a key called companyDescription. The value associated with companyDescription is an arbitrary JavaScript object.

If I query my table like this

select data->>'companyDescription' from companies where data is not null;

I get rows like this

{"ops":[{"insert":"\n"}]}

I am trying to update all rows in the table so that the companyDescription values will be wrapped in another JSON-encoded JavaScript object in the following manner:

{"type":"quill","content":{"ops":[{"insert":"\n"}]}}

Here's what I have tried, but I think it won't work because the ->> operator is for selecting some JSON field as text, and indeed it fails with a syntax error.

update companies
set data->>'companyDescription' = CONCAT(
  '{"type":"quill","content":',
  (select data->>'companyDescription' from companies),
  '}'
);

What is the correct way to do this?

Upvotes: 0

Views: 173

Answers (1)

Pavel Stehule
Pavel Stehule

Reputation: 45950

You can use a function jsonb_set. Currently XML or JSON values are immutable. You cannot to update some parts of these values. You can replace these values by some new modified value.

postgres=# select * from test;
┌──────────────────────────────────────────────────────────────────────┐
│                                  v                                   │
╞══════════════════════════════════════════════════════════════════════╡
│ {"companyId": 10, "companyDescription": {"ops": [{"insert": "\n"}]}} │
└──────────────────────────────────────────────────────────────────────┘
(1 row)

postgres=# select jsonb_build_object('type', 'quill', 'content', v->'companyDescription') from test;
┌───────────────────────────────────────────────────────────┐
│                    jsonb_build_object                     │
╞═══════════════════════════════════════════════════════════╡
│ {"type": "quill", "content": {"ops": [{"insert": "\n"}]}} │
└───────────────────────────────────────────────────────────┘
(1 row)

postgres=# select jsonb_set(v, ARRAY['companyDescription'], jsonb_build_object('type', 'quill', 'content', v->'companyDescription')) from test;
┌────────────────────────────────────────────────────────────────────────────────────────────────────┐
│                                             jsonb_set                                              │
╞════════════════════════════════════════════════════════════════════════════════════════════════════╡
│ {"companyId": 10, "companyDescription": {"type": "quill", "content": {"ops": [{"insert": "\n"}]}}} │
└────────────────────────────────────────────────────────────────────────────────────────────────────┘
(1 row)

So you final statement can looks like:

update companies
  set data = jsonb_set(data::jsonb, 
                       ARRAY['companyDescription'], 
                       jsonb_build_object('type', 'quill', 
                                          'content', data->'companyDescription'))
  where data is not null;

Upvotes: 1

Related Questions