如何从JSON中可靠获取SkipEndUserAcceptance值(缺失时设为false)
解决方案:从JSONB中稳定获取SkipEndUserAcceptance布尔值
问题分析
原查询表达式返回结果不稳定的核心原因:
- 数据中
SkipEndUserAcceptance的Value同时存在布尔类型(如true/false)和字符串类型(如"true"),@>操作符会严格匹配值的类型,导致布尔类型的条目无法被识别。 - 未处理该属性不存在时默认返回
false的场景。
可行SQL查询
SELECT id, COALESCE( (SELECT CASE WHEN p.properties->>'Value' IN ('true', 'True', 'TRUE') THEN true WHEN p.properties->>'Value' IN ('false', 'False', 'FALSE') THEN false ELSE (p.properties->'Value')::boolean END FROM jsonb_array_elements(t.templatevalues::jsonb->'complexTypeProperties') p WHERE p.properties->>'AttributeName' = 'SkipEndUserAcceptance' LIMIT 1), false) AS "SkipEndUserAcceptance" FROM your_table t;
语句说明
- 展开JSON数组:
jsonb_array_elements(t.templatevalues::jsonb->'complexTypeProperties')将complexTypeProperties数组拆分为独立的行,方便遍历筛选。 - 筛选目标属性:通过
WHERE p.properties->>'AttributeName' = 'SkipEndUserAcceptance'精准定位到目标属性条目。 - 统一类型转换:
- 针对字符串形式的
"true"/"false",直接匹配转换为布尔值; - 针对原生布尔类型的
Value,直接强制转换为布尔类型。
- 针对字符串形式的
- 默认值处理:
COALESCE(..., false)确保当目标属性不存在时,返回默认值false。 - 去重保障:
LIMIT 1避免同一记录中存在多个SkipEndUserAcceptance条目时返回多值,只取第一个有效结果。
示例验证
示例数据
| 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"}}]} |
期望输出
| id | SkipEndUserAcceptance |
|---|---|
| 1 | true |
| 2 | true |
| 3 | true |
| 4 | false |
| 5 | true |
| 6 | false |
内容的提问来源于stack exchange,提问作者executable
相关产品推荐
相关产品推荐

