You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从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;

语句说明

  1. 展开JSON数组:jsonb_array_elements(t.templatevalues::jsonb->'complexTypeProperties') 将complexTypeProperties数组拆分为独立的行,方便遍历筛选。
  2. 筛选目标属性:通过WHERE p.properties->>'AttributeName' = 'SkipEndUserAcceptance'精准定位到目标属性条目。
  3. 统一类型转换:
    • 针对字符串形式的"true"/"false",直接匹配转换为布尔值;
    • 针对原生布尔类型的Value,直接强制转换为布尔类型。
  4. 默认值处理:COALESCE(..., false)确保当目标属性不存在时,返回默认值false。
  5. 去重保障:LIMIT 1避免同一记录中存在多个SkipEndUserAcceptance条目时返回多值,只取第一个有效结果。

示例验证

示例数据

idtemplatevalues
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"}}]}

期望输出

idSkipEndUserAcceptance
1true
2true
3true
4false
5true
6false

内容的提问来源于stack exchange,提问作者executable

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 06:32:11