PostgreSQL 9.2 - Convert TEXT json string to type json/hstore

前端 未结 6 911
难免孤独
难免孤独 2021-01-31 07:15

I have a TEXT column containing valid JSON string.

CREATE TABLE users(settings TEXT);

INSERT INTO users VALUES (\'{\"language\":\"en\",\"gender\":\         


        
6条回答
  •  暖寄归人
    2021-01-31 08:12

    So I had an issue where the text was JSON. If you have this issue use this query instead. Where COLUMN is the column that contains the JSONB or JSON datatype and ATTRIBUTE is the attribute of the JSON that is a string, that you want converted into JSON.

    The text will look like this, "{\"junk5\": 283774663, \"junk2\": 0, \"junk1\": 1218478497, \"junk3\":1923, \"junk4\": 63278342}"

    SELECT CAST(TRIM(both '"' from jsonstring) as JSON)
    FROM (
        SELECT REPLACE(cast(COLUMN->'ATTRIBUTE' as text), '\"', '"')
        as jsonString from TABLE where cast(COLUMN->'ATTRIBUTE' as text)LIKE '%\\%'
    ) as JSON_CONVERTING
    

提交回复
热议问题