从JSON中获取“SkipEndUserAcceptance”的值

huangapple go评论57阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年6月22日 16:05:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76529756.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定