代码之家  ›  专栏  ›  技术社区  ›  Alkis Kalogeris

Postgres 9.5更新内部json字段

  •  0
  • Alkis Kalogeris  · 技术社区  · 7 年前

    {
      "logo": {
        "url": "https://foo.bar"
        ...
      }
      ...
    }
    

    对象具有更多字段,但 url

    1 回复  |  直到 7 年前
        1
  •  1
  •   a_horse_with_no_name    7 年前

    你不能这样做 json 仅适用于 jsonb (但您可以轻松地转换现有值)

    具有 jsonb_set '{logo,url}' :

    以下内容:

    with t (data) as (
      values
        ('{
            "logo": { "url": "https://foo.bar", "something" : "some value"}, 
            "other" : { "one": "two"}
          }'::jsonb
        )
    )
    select jsonb_set(data, '{logo,url}', to_jsonb('http://bar.foo'::text))
    from t;
    

    WITH

    返回:

    jsonb_set                                                                              
    ---------------------------------------------------------------------------------------
    {"logo": {"url": "http://bar.foo", "something": "some value"}, "other": {"one": "two"}}
    

    url 属性被替换,其他所有内容保持原样。

    如果您的列真的是json,只需使用 your_column::jsonb jsonb_set()