使用 jsoncol#>>'{key}' 和 (jsoncol#>'{key}')::text 有什么区别吗?

is there any difference between using jsoncol#>>'{key}' and (jsoncol#>'{key}')::text?

选项 1 - 将 json 提取为文本:jsoncol#>>'{key}'

选项 2 - 将 json 值转换为文本:(jsoncol#>'{key}')::text

有区别吗?

它们的输出之间存在一些差异:它们处理 json(和 jsonbjson 'null' 和字符串(f.ex。json '"foo"')值的方式不同:

select j #>> '{key}'        "j #>> '{key}'",
       (j #> '{key}')::text "(j #> '{key}')::text"
from (values (json '{"key":null}'),
                  ('{"key":true}'),
                  ('{"key":false}'),
                  ('{"key":"foo"}'),
                  ('{"key":12.34}'),
                  ('{"key":["array"]}'),
                  ('{"key":{"obj":"ect"}}')) v(j)

将产生:

| j #>> '{key}'   | (j #> '{key}')::text |
+-----------------+----------------------+
| NULL            | 'null'               | --> first is true SQL NULL
| 'true'          | 'true'               |
| 'false'         | 'false'              |
| 'foo'           | '"foo"'              | --> note the quotes
| '12.34'         | '12.34'              |
| '["array"]'     | '["array"]'          |
| '{"obj":"ect"}' | '{"obj":"ect"}'      |

(jsoncol #> '{key}')::text 变体总是 转换为文本,这意味着它将显示其 JSON 值的文本表示,而 jsoncol #>> '{key}' 已定义对文本进行一些转换