【发布时间】:2017-03-08 17:01:40
【问题描述】:
我进行了广泛的搜索(在 Postgres 文档以及 Google 和 SO 中)以找到在表中的实际 JSON 列上使用的 JSON 函数的示例。
这是我的问题:我正在尝试使用 jsonb_to_recordset() 从列中的 JSON 对象数组中提取键值,但出现语法错误。当我将对象逐字传递给函数时,它工作正常:
按字面传递 JSON:
select *
from jsonb_to_recordset('[
{ "id": 0, "name": "400MB-PDF.pdf", "extension": ".pdf",
"transferId": "ap31fcoqcajjuqml6rng"},
{ "id": 0, "name": "1000MB-PDF.pdf", "extension": ".pdf",
"transferId": "ap31fcoqcajjuqml6rng"}
]') as f(name text);`
结果:
400MB-PDF.pdf
1000MB-PDF.pdf
它提取键“name”的值。
这是列中的 JSON,使用以下方法提取:
select journal.data::jsonb#>>'{context,data,files}'
from journal
where id = 'ap32bbofopvo7pjgo07g';
导致:
[ { "id": 0, "name": "400MB-PDF.pdf", "extension": ".pdf",
"transferId": "ap31fcoqcajjuqml6rng"},
{ "id": 0, "name": "1000MB-PDF.pdf", "extension": ".pdf",
"transferId": "ap31fcoqcajjuqml6rng"}
]
但是当我尝试像这样将 jsonb#>>'{context,data,files}' 传递给 jsonb_to_recordset() 时:
select id,
journal.data::jsonb#>>::jsonb_to_recordset('{context,data,files}') as f(name text)
from journal
where id = 'ap32bbofopvo7pjgo07g';
我收到语法错误。我尝试了不同的方法,但每次都抱怨语法错误:
版本: x86_64-unknown-linux-gnu 上的 PostgreSQL 9.4.10,由 gcc (Ubuntu 4.8.2-19ubuntu1) 4.8.2 编译,64 位
【问题讨论】:
标签: sql json postgresql