Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql 从jsonb字段构建jsonb数组_Postgresql_Jsonb - Fatal编程技术网

Postgresql 从jsonb字段构建jsonb数组

Postgresql 从jsonb字段构建jsonb数组,postgresql,jsonb,Postgresql,Jsonb,我有一个列选项,类型为jsonb,格式为{“names”:[“name1”,“name2”]},它是用 UPDATE table1 t1 SET options = (SELECT jsonb_build_object('names', names) FROM table2 t2 WHERE t2.id= t1.id) 其中,名称的类型为jsonb数组 从表2中选择jsonb_类型(名称)givearray 现在我想将names的值提取为jsonb数组。但质疑 SELECT jsonb_bui

我有一个列
选项
,类型为jsonb,格式为
{“names”:[“name1”,“name2”]}
,它是用

UPDATE table1 t1 SET options = (SELECT jsonb_build_object('names', names) FROM table2 t2 WHERE t2.id= t1.id)
其中,
名称
的类型为jsonb数组

从表2中选择jsonb_类型(名称)
give
array

现在我想将
names
的值提取为jsonb数组。但质疑

SELECT jsonb_build_array(options->>'names') FROM table 
给了我
[“[\“name1\”,“\“name2\”]”]
,而我期望
[“name1”,“name2”]


如何以正确的格式获取值?

操作符将以正确转义文本的形式返回字段的值(在您的情况下是JSON数组)。您要找的是
->
操作符


但是,请注意,在上面使用
jsonb_build_数组
,将返回包含原始数组的数组,这可能不是您想要的;只需使用
选项->“名称”
就可以满足您的需要。

实际上,您不需要使用
jsonb\u build\u array()
函数

使用
从表中选择选项->“名称”这将解决您的问题

jsonb\u build\u array()
用于从
jsonb
对象生成数组。你走错了路。这就是为什么会得到这样的字符串
[“[\“name1\”,“\“name2\”]”]

尝试执行以下示例SQL脚本:

select j->'names'from(select'{“names”:[“name1”,“name2”]}'::JSONB作为j)作为一个