Postgres: How to convert a json string to text?

后端 未结 5 1300
遥遥无期
遥遥无期 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 05:41

    There is no way in PostgreSQL to deconstruct a scalar JSON object. Thus, as you point out,

    select  length(to_json('Some "text"'::TEXT) ::TEXT);
    

    is 15,

    The trick is to convert the JSON into an array of one JSON element, then extract that element using ->>.

    select length( array_to_json(array[to_json('Some "text"'::TEXT)])->>0 );
    

    will return 11.

提交回复
热议问题