简体   繁体   English

如何更新 PostgreSQL 中的 JSON 字段?

[英]How do I update a JSON field in PostgreSQL?

I have a table with a column called data that contains some JSON.我有一个表,其中包含一个名为data的列,其中包含一些 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 .如果表中任何给定行的data列不是 null,它将包含一个 JSON 编码的 object 和一个名为companyDescription的键。 The value associated with companyDescription is an arbitrary JavaScript object.companyDescription关联的值是任意 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:我正在尝试更新表中的所有行,以便companyDescription值将以下列方式包装在另一个 JSON 编码的 JavaScript object 中:

{"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.这是我尝试过的,但我认为它不起作用,因为->>运算符用于选择一些 JSON 字段作为text ,并且确实失败并出现语法错误。

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

What is the correct way to do this?这样做的正确方法是什么?

You can use a function jsonb_set .您可以使用 function jsonb_set Currently XML or JSON values are immutable.目前XMLJSON值是不可变的。 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM