什么是postgresql标量JSON?

时间:2017-11-28 16:57:51

标签: postgresql

我有一个JSON列,其中包含从外部应用程序插入的一些数据。

以下是这样一个json的例子:

{"static":[{"key":"erferf","value":"refreref"},{"key":"aaa","value":"aaa"}],"user_data":[{"key":"aaaa","value":"##CUSTOM001##"}],"dynamic":[{"key":"aaa","title":"aaaa","type":"string","required":true}]}

当我尝试通过以下语法选择第一个键时

select some_json_column->>'static'

我得到了NULL。

我认为它与SCALAR Json有关。

任何人都可以告诉我我做错了吗?

Shaby

1 个答案:

答案 0 :(得分:0)

您的json数据不是标量,它是复合数据。看看:

-- 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;

顺便说一句,我无法重现你的错误:

--It'll return json object as expected
WITH sample (json_column) AS (
    VALUES ('{"static":[{"key":"erferf","value":"refreref"},{"key":"aaa","value":"aaa"}],"user_data":[{"key":"aaaa","value":"##CUSTOM001##"}],"dynamic":[{"key":"aaa","title":"aaaa","type":"string","required":true}]}'::JSON)
)
SELECT json_column->>'static' FROM sample;  

有关json here的更多信息。