解析jsonb字段 - PostgreSQL

时间:2017-06-28 14:01:18

标签: postgresql

我在PostgreSQL数据库中有一个jsonb类型的字段 示例:

{"timestamp":"2016-12-14T04:15:04.836Z","receiptResult":{"status":"successful","timestamp":"2016-12-14T04:15:04.739Z","notes":"Customer Accepted"}}

我怎样才能在select语句中返回“notes”,我试过了:

SELECT data::json->>'notes' as notes

但如果我使用的话,则不会返回任何内容:

SELECT data::json->'receiptResult' as notes;

它返回:

{"status":"successful","timestamp":"2016-114T04:15:04.739Z","notes":"Customer Accepted"}

但我只需要“注释”之后的文字。

1 个答案:

答案 0 :(得分:0)

内部键receiptResult有另一个JSON对象,您无法在顶级访问它。试试这个:

WITH sample AS (
    SELECT '{"timestamp":"2016-12-14T04:15:04.836Z","receiptResult":{"status":"successful","timestamp":"2016-12-14T04:15:04.739Z","notes":"Customer Accepted"}}'::jsonb AS my_column
)
SELECT my_column->'receiptResult'->>'notes' FROM sample;

如您所见,->运算符将值作为JSONB返回,->>运算符作为文本返回。

更多信息here

相关问题