0

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

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

有什么区别吗?

4

1 回答 1

1

它们的输出之间存在一些差异:它们以不同的方式处理json(and jsonb)json '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}'被定义为对文本进行一些转换

于 2015-01-29T12:03:27.253 回答