英文:
Retrieving the value of "SkipEndUserAcceptance" from a JSON
问题
我尝试从JSON中检索信息,但信息的具体位置可能会变化。这里是我有的数据示例:
id | templatevalues |
---|---|
1 | {"complexTypeProperties":[{"properties":{"Value":"NoDisruption","AttributeName":"Urgency"}},{"properties":{"Value":"SingleUser","AttributeName":"ImpactScope"}},{"properties":{"Value":"448928","AttributeName":"RegisteredForActualService"}},{"properties":{"Value":"10146","AttributeName":"Category"}},{"properties":{"Value":true,"AttributeName":"SkipEndUserAcceptance"}}]} |
2 | {"complexTypeProperties":[{"properties":{"AttributeName":"RegisteredForActualService"}},{"properties":{"Value":"SingleUser","AttributeName":"ImpactScope"}},{"properties":{"Value":"NoDisruption","AttributeName":"Urgency"}},{"properties":{"Value":"10154","AttributeName":"Category"}},{"properties":{"Value":"true","AttributeName":"SkipEndUserAcceptance"}}]} |
3 | {"complexTypeProperties":[{"properties":{"Value":"SingleUser","AttributeName":"ImpactScope"}},{"properties":{"Value":"NoDisruption","AttributeName":"Urgency"}},{"properties":{"Value":"721846","AttributeName":"RegisteredForActualService"}},{"properties":{"Value":"10146","AttributeName":"Category"}},{"properties":{"Value":"true","AttributeName":"SkipEndUserAcceptance"}}]} |
4 | {"complexTypeProperties":[{"properties":{"Value":"SingleUser","AttributeName":"ImpactScope"}},{"properties":{"Value":"SlightDisruption","AttributeName":"Urgency"}},{"properties":{"Value":"2854102","AttributeName":"RegisteredForActualService"}},{"properties":{"Value":"10153","AttributeName":"Category"}},{"properties":{"Value":"435331","AttributeName":"ServiceDeskGroup"}}]} |
5 | {"complexTypeProperties":[{"properties":{"Value":"NoDisruption","AttributeName":"Urgency"}},{"properties":{"Value":"SingleUser","AttributeName":"ImpactScope"}},{"properties":{"Value":"10146","AttributeName":"Category"}},{"properties":{"Value":"597224","AttributeName":"RegisteredForActualService"}},{"properties":{"Value":true,"AttributeName":"SkipEndUserAcceptance"}}]} |
6 | {"complexTypeProperties":[{"properties":{"Value":"NoDisruption","AttributeName":"Urgency"}},{"properties":{"Value":"SingleUser","AttributeName":"ImpactScope"}},{"properties":{"Value":"10146","AttributeName":"Category"}},{"properties":{"Value":"597224","AttributeName":"RegisteredForActualService"}},{"properties":{"Value":false,"AttributeName":"SkipEndUserAcceptance"}}]} |
我需要从JSON中提取“SkipEndUserAcceptance”的值。此值可以是“false”或“true”。目前,我正在使用以下表达式:
TEMPLATEVALUES::jsonb -> 'complexTypeProperties' @> '[{"properties":{"AttributeName":"SkipEndUserAcceptance","Value":"true"}}]' AS "SkipEndUserAcceptance"
来提取它。然而,返回的值似乎是不可预测的。
请问你能提供一个检索“SkipEndUserAcceptance”值的解决方案吗?此外,如果属性“SkipEndUserAcceptance”不存在,它应该被设置为“false”。
当前输出:
id | SkipEndUserAcceptance |
---|---|
1 | false |
2 | true |
3 | true |
4 | false |
5 | false |
6 | false |
期望输出:
id | SkipEndUserAcceptance |
---|---|
1 | true |
2 | true |
3 | true |
4 | false |
5 | true |
6 | false |
fiddle链接:https://www.db-fiddle.com/f/6AacERW43QoJ5Ggb3X4Dys/0
英文:
I'm attempting to retrieve information from a JSON, but the specific location of the information may vary. Here is an example of the data I have:
id | templatevalues |
---|---|
1 | {"complexTypeProperties":[{"properties":{"Value":"NoDisruption","AttributeName":"Urgency"}},{"properties":{"Value":"SingleUser","AttributeName":"ImpactScope"}},{"properties":{"Value":"448928","AttributeName":"RegisteredForActualService"}},{"properties":{"Value":"10146","AttributeName":"Category"}},{"properties":{"Value":true,"AttributeName":"SkipEndUserAcceptance"}}]} |
2 | {"complexTypeProperties":[{"properties":{"AttributeName":"RegisteredForActualService"}},{"properties":{"Value":"SingleUser","AttributeName":"ImpactScope"}},{"properties":{"Value":"NoDisruption","AttributeName":"Urgency"}},{"properties":{"Value":"10154","AttributeName":"Category"}},{"properties":{"Value":"true","AttributeName":"SkipEndUserAcceptance"}}]} |
3 | {"complexTypeProperties":[{"properties":{"Value":"SingleUser","AttributeName":"ImpactScope"}},{"properties":{"Value":"NoDisruption","AttributeName":"Urgency"}},{"properties":{"Value":"721846","AttributeName":"RegisteredForActualService"}},{"properties":{"Value":"10146","AttributeName":"Category"}},{"properties":{"Value":"true","AttributeName":"SkipEndUserAcceptance"}}]} |
4 | {"complexTypeProperties":[{"properties":{"Value":"SingleUser","AttributeName":"ImpactScope"}},{"properties":{"Value":"SlightDisruption","AttributeName":"Urgency"}},{"properties":{"Value":"2854102","AttributeName":"RegisteredForActualService"}},{"properties":{"Value":"10153","AttributeName":"Category"}},{"properties":{"Value":"435331","AttributeName":"ServiceDeskGroup"}}]} |
5 | {"complexTypeProperties":[{"properties":{"Value":"NoDisruption","AttributeName":"Urgency"}},{"properties":{"Value":"SingleUser","AttributeName":"ImpactScope"}},{"properties":{"Value":"10146","AttributeName":"Category"}},{"properties":{"Value":"597224","AttributeName":"RegisteredForActualService"}},{"properties":{"Value":true,"AttributeName":"SkipEndUserAcceptance"}}]} |
6 | {"complexTypeProperties":[{"properties":{"Value":"NoDisruption","AttributeName":"Urgency"}},{"properties":{"Value":"SingleUser","AttributeName":"ImpactScope"}},{"properties":{"Value":"10146","AttributeName":"Category"}},{"properties":{"Value":"597224","AttributeName":"RegisteredForActualService"}},{"properties":{"Value":false,"AttributeName":"SkipEndUserAcceptance"}}]} |
I need to retrieve the value of "SkipEndUserAcceptance" from a JSON. This value can be either "false" or "true". Currently, I'm using the expression :
TEMPLATEVALUES::jsonb -> 'complexTypeProperties' @> '[{"properties":{"AttributeName":"SkipEndUserAcceptance","Value":"true"}}]' AS "SkipEndUserAcceptance"
to retrieve it. However, the returned value appears to be unpredictable.
Could you please suggest a solution for retrieving the value of "SkipEndUserAcceptance"? Additionally, if the attribute "SkipEndUserAcceptance" is not present, it should be set to "false".
Current output :
id | SkipEndUserAcceptance |
---|---|
1 | false |
2 | true |
3 | true |
4 | false |
5 | false |
6 | false |
Expected output :
id | SkipEndUserAcceptance |
---|---|
1 | true |
2 | true |
3 | true |
4 | false |
5 | true |
6 | false |
fiddle : https://www.db-fiddle.com/f/6AacERW43QoJ5Ggb3X4Dys/0
答案1
得分: 1
使用jsonb_to_recordset从数组创建表格:
SELECT id
, COALESCE((MAX(properties ->> 'Value')
FILTER (WHERE properties ->> 'AttributeName' = 'SkipEndUserAcceptance'))::BOOL
, FALSE) AS skipenduseracceptance
FROM tbl_temp
, JSONB_TO_RECORDSET(templatevalues -> 'complexTypeProperties') jtr(properties JSONB)
GROUP BY id
ORDER BY id;
英文:
Use jsonb_to_recordset to create a table from the array:
SELECT id
, COALESCE((MAX(properties ->> 'Value')
FILTER (WHERE properties ->> 'AttributeName' = 'SkipEndUserAcceptance'))::BOOL
, FALSE) AS skipenduseracceptance
FROM tbl_temp
, JSONB_TO_RECORDSET(templatevalues -> 'complexTypeProperties') jtr(properties JSONB)
GROUP BY id
ORDER BY id;
答案2
得分: 1
使用这个标量子查询来提取SkipEndUserAcceptance
的值,并将其合并为false
以获得预期结果。以下是代码部分的翻译:
select id,
coalesce((
select (j ->> 'Value')::boolean
from jsonb_path_query(templatevalues, '$.complexTypeProperties[*].properties') as j
where j ->> 'AttributeName' = 'SkipEndUserAcceptance'
), false) as "SkipEndUserAcceptance"
from the_table;
查看演示。
英文:
Use this scalar subquery
(
select (j ->> 'Value')::boolean
from jsonb_path_query(templatevalues, '$.complexTypeProperties[*].properties') as j
where j ->> 'AttributeName' = 'SkipEndUserAcceptance'
)
to extract the SkipEndUserAcceptance
value and coalesce
it to false
for the expected result. Here it is:
select id,
coalesce((
select (j ->> 'Value')::boolean
from jsonb_path_query(templatevalues, '$.complexTypeProperties[*].properties') as j
where j ->> 'AttributeName' = 'SkipEndUserAcceptance'
), false) as "SkipEndUserAcceptance"
from the_table;
See Demo.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论