简体   繁体   English

如何从 postgres 文本列 json 字符串数组中删除值?

[英]How to remove a value from a postgres text column json string array?

I have a postgresql table with a column that is a text datatype.我有一个 postgresql 表,其中有一列是文本数据类型。 This column value is a JSON string array.此列值是 JSON 字符串数组。 These values could be in any order such as这些值可以按任何顺序排列,例如

["a", "b", "c"] 

or或者

["c", "a", "b", "d"] 

or或者

["a"]

Now one of these values "b" is no longer used and needs to be removed from each record in the table.现在这些值之一"b"不再使用,需要从表中的每条记录中删除。

so ["a", "b", "c"] should be converted to ["a", "c"]所以["a", "b", "c"]应该转换为["a", "c"]

I have looked into using one of the postgres functions array_remove() but haven't been able to get it to work.我已经研究过使用其中一个 postgres函数array_remove() 但无法让它工作。

Is it possible to convert to an array type and convert back to a string after using remove?是否可以在使用remove后转换为数组类型并转换回字符串?

Use a jsonb value (cast your text if necessary) and the - operator:使用jsonb值(必要时转换您的文本)和-运算符:

UPDATE table SET col = col::jsonb - 'a';

( online demo ) 在线演示

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

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