我有这样的文字:
("{""A"":""0/000"",""B"":2}")
我想将它转换为像这样的jsonb:
{"A":"0/000","B":2}
我该怎么做?
答案 0 :(得分:1)
假设您要将text
列的值转换为单独的jsonb
列,您可以使用函数substring
和replace
,并将结果转换为jsonb
如下:
INSERT INTO my_table (id, text_col, jsonb_col) VALUES
(1, '("{""A"":""0/000"",""B"":2}")', null);
UPDATE my_table SET
jsonb_col = replace(substring(text_col, '{.*}'), '""', '"')::jsonb
WHERE id = 1;
SELECT id, text_col, jsonb_col FROM my_table WHERE id = 1;
id | text_col | jsonb_col
----+-------------------------------+------------------------
1 | ("{""A"":""0/000"",""B"":2}") | {"A": "0/000", "B": 2}
(1 row)