错误的原因是什么是从对象中提取元素的原因。

发布于 2025-02-05 17:30:58 字数 1039 浏览 2 评论 0原文

我需要创建一些请求女巫,如果它为null,则会添加一个JSON对象:

{
    "json_object_1": [
        {
            "x": 0,
            "y": 0,
            "value": 0
        }
    ],
      *a new object must be added here*
}

如果它不是null,则将键值对添加到嵌套对象中:

{
    "json_object_1": [
        {
            "x": 0,
            "y": 0,
            "r": 0
        }
    ],
    "json_object_2":[
     {
         "x": 0,
         "x":0,
         *new key-value pair*
     }
]
}

我写了我的请求,但仅在第一次尝试后起作用。之后,我会收到一个错误:SQL错误[22023]:错误:无法从对象中提取元素。此错误的原因是什么?我该如何解决?

    update table 
set json_column = (
case
    when json_column = json_column - 'json_object' then 
    json_column - 'json_object' || jsonb_build_object('json_object', jsonb_build_object('value', 3))
    else jsonb_set(
        json_column, 
        '{json_object}', 
        (select jsonb_agg(elem ||'{"value": 3}')
         from jsonb_array_elements(json_column->'json_object') AS elem))
    end
)
where json_column is not null;

I need to create some request witch will add an json object if it is null:

{
    "json_object_1": [
        {
            "x": 0,
            "y": 0,
            "value": 0
        }
    ],
      *a new object must be added here*
}

or add a key-value pair into a nested object if it is not null:

{
    "json_object_1": [
        {
            "x": 0,
            "y": 0,
            "r": 0
        }
    ],
    "json_object_2":[
     {
         "x": 0,
         "x":0,
         *new key-value pair*
     }
]
}

I wrote my request but it works only after the first try. After that I get an error: SQL Error [22023]: ERROR: cannot extract elements from an object. What is the reason of this error and how can I fix it?

    update table 
set json_column = (
case
    when json_column = json_column - 'json_object' then 
    json_column - 'json_object' || jsonb_build_object('json_object', jsonb_build_object('value', 3))
    else jsonb_set(
        json_column, 
        '{json_object}', 
        (select jsonb_agg(elem ||'{"value": 3}')
         from jsonb_array_elements(json_column->'json_object') AS elem))
    end
)
where json_column is not null;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文