8.14.1. JSON Input and Output Syntax
8.14.1.JSON输入和输出语法
The input/output syntax for the JSON data types is as specified in RFC 7159.
RFC 7159定义了JSON数据类型的输入和输出语法。
 
The following are all valid json (or jsonb) expressions:
以下为json(或jsonb)可用的表达式:
 
-- Simple scalar/primitive value
-- Primitive values can be numbers, quoted strings, true, false, or
null
SELECT '5'::json;
-- Array of zero or more elements (elements need not be of same type)
SELECT '[1, 2, "foo", null]'::json;
-- Object containing pairs of keys and values
-- Note that object keys must always be quoted strings
SELECT '{"bar": "baz", "balance": 7.77, "active": false}'::json;
-- Arrays and objects can be nested arbitrarily
SELECT '{"foo": [true, "bar"], "tags": {"a": 1, "b": null}}'::json;
 
As previously stated, when a JSON value is input and then printed without any additional processing,json outputs the same text that was input, while jsonb does not preserve semantically-insignificant details such as whitespace. For example, note the differences here:
如前所述,当输入JSON值然后在不进行任何其他处理的情况下进行输出时,json将输出与输入相同的文本,而jsonb不会保留语义上无关紧要的细节,例如空格。 例如,请注意此处的区别:
 
SELECT '{"bar": "baz", "balance": 7.77, "active":false}'::json;
json
-------------------------------------------------
{"bar": "baz", "balance": 7.77, "active":false}
(1 row)
SELECT '{"bar": "baz", "balance": 7.77, "active":false}'::jsonb;
jsonb
--------------------------------------------------
{"bar": "baz", "active": false, "balance": 7.77}
(1 row)
 
One semantically-insignificant detail worth noting is that in jsonb, numbers will be printed according to the behavior of the underlying numeric type. In practice this means that numbers entered with notation will be printed without it, for example:
值得注意的一个语义无关紧要的细节是,在jsonb中,数字将根据基础数字类型的行为进行打印。 实际上,这意味着使用E记号输入的数字将以普通格式打印,例如:
 
SELECT '{"reading": 1.230e-5}'::json, '{"reading":1.230e-5}'::jsonb;
json | jsonb
-----------------------+-------------------------
{"reading": 1.230e-5} | {"reading": 0.00001230}
(1 row)
 
However, jsonb will preserve trailing fractional zeroes, as seen in this example, even though those are semantically insignificant for purposes such as equality checks.
但是,jsonb将保留尾随零,如本例所示,即使它们在语义上无意义(例如,相等性检查)也是如此。
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐