Postgres: How to convert a json string to text?

后端 未结 5 1283
遥遥无期
遥遥无期 2020-11-30 05:35

Json value may consist of a string value. eg.:

postgres=# SELECT to_json(\'Some \"text\"\'::TEXT);
     to_json
-----------------
 \"Some \\\"text\\\"\"
         


        
5条回答
  •  既然无缘
    2020-11-30 06:01

    Mr. Curious was curious about this as well. In addition to the #>> '{}' operator, in 9.6+ one can get the value of a jsonb string with the ->> operator:

    select to_jsonb('Some "text"'::TEXT)->>0;
      ?column?
    -------------
     Some "text"
    (1 row)
    

    If one has a json value, then the solution is to cast into jsonb first:

    select to_json('Some "text"'::TEXT)::jsonb->>0;
      ?column?
    -------------
     Some "text"
    (1 row)
    

提交回复
热议问题