重命名JSON中的属性 [英] Rename attribute in a JSON
问题描述
你好,这是我的json
Hello here is my json
{
"name":'test',
"options": {
"Repartition": "Active",
"Satellite": "No"
}
}
我想在卫星"中将卫星"重命名为's',但是我没有成功.
I'd like to rename "Satellite" in "Satellites" with an 's' but I don't managed to.
我尝试过:
UPDATE "Liaison" set content = content->>'options' - 'Satellite' || jsonb_build_object('Satellites', content->>'options'->'Satellite') where id =52056
但是我有一个错误:
操作员不是唯一的:未知-未知
operator is not unique: unknown - unknown
这是我的桌子:
ID | content
---------------------
52056 | the json
推荐答案
->>
将元素返回为text
,因此您将无法在其上应用JSON运算符.您需要使用->
来将(sub)元素作为JSON返回,并使用jsonb_set()
来更改内容中的options
元素:
->>
returns the element as text
, so you lose the ability to apply JSON operators on it. You need to use ->
to return the (sub) element as JSON and use jsonb_set()
to change the options
element in the content:
UPDATE "Liaison"
set content = jsonb_set(content, array['options'],
(content -> 'options') - 'Satellite'||
jsonb_build_object('Satellites', content -> 'options' -> 'Satellite'))
where id = 52056;
-
运算符仅适用于JSONB,不适用于JSON.因此,如果您的列确实是JSON,则需要将中间结果转换为JSONB.
The -
operator however only works for JSONB, not JSON. So if your columns is indeed JSON, you need to cast the intermediate result to JSONB.
在线示例: http://rextester.com/EXTS36112
另一种选择是使用jsonb_set()
创建新元素,并使用#-
删除旧元素.
Another option is to use jsonb_set()
to create a new element and #-
to remove the old.
UPDATE liaison
set content = jsonb_set(
content,
array['options','Satellites'],
(content #> array['options','Satellite']), true
) #- array['options','Satellite']
where id = 52056;
这篇关于重命名JSON中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!