将此文本转换为jsonb?

时间:2017-09-29 03:20:47

标签: postgresql text jsonb

我有这样的文字:

("{""A"":""0/000"",""B"":2}")

我想将它转换为像这样的jsonb:

{"A":"0/000","B":2}

我该怎么做?

1 个答案:

答案 0 :(得分:1)

假设您要将text列的值转换为单独的jsonb列,您可以使用函数substringreplace,并将结果转换为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)