7

Given the following json:

{  "foo": [    {      "bar": true    },    {      "bar": true    }  ]}

How can I select the following:

{  "foo": [    {      "bar": false    },    {      "bar": false    }  ]}

?

So far I've figured out how to manipulate a single array value:

SELECT  jsonb_set(    '{      "foo": [        {          "bar": true        },        {          "bar": true        }      ]    }'::jsonb, '{foo,0,bar}', to_jsonb(false)  )

But how do I setall elements within an array?

Paul P's user avatar
Paul P
4,0372 gold badges15 silver badges28 bronze badges
askedFeb 16, 2017 at 14:37
Andy N's user avatar

2 Answers2

9

There is no standard function to update json array elements by key.A custom function is probably the simplest way to solve the problem:

create or replace function update_array_elements(arr jsonb, key text, value jsonb)returns jsonb language sql as $$    select jsonb_agg(jsonb_build_object(k, case when k <> key then v else value end))    from jsonb_array_elements(arr) e(e),     lateral jsonb_each(e) p(k, v)$$;select update_array_elements('[{"bar":true},{"bar":true}]'::jsonb, 'bar', 'false');      update_array_elements---------------------------------- [{"bar": false}, {"bar": false}](1 row)

Your query may look like this:

with a_data(js) as (values(    '{        "foo": [          {            "bar": true          },          {            "bar": true          }        ]    }'::jsonb))select    jsonb_set(js, '{foo}', update_array_elements(js->'foo', 'bar', 'false'))    from a_data;                 jsonb_set                 ------------------------------------------- {"foo": [{"bar": false}, {"bar": false}]}(1 row)
answeredFeb 16, 2017 at 18:32
klin's user avatar
Sign up to request clarification or add additional context in comments.

Comments

9

You might want to kill two birds with one stone - update existing key in every object in the array or insert the key with a given value.jsonb_set is a perfect match here, but it requires us to pass the index of each object, so we have to iterate over the array first.

The implementation is HIGHLY inspired by klin's answer, which didn't solve my problem (which was updating and inserting) and didn't work if there were multiple keys in the object.So, the implementation is as follows:

-- the params are the same as in aforementioned `jsonb_set`CREATE OR REPLACE FUNCTION update_array_elements(target jsonb, path text[], new_value jsonb)  RETURNS jsonb language sql AS $$    -- aggregate the jsonb from parts created in LATERAL    SELECT jsonb_agg(updated_jsonb)    -- split the target array to individual objects...    FROM jsonb_array_elements(target) individual_object,    -- operate on each object and apply jsonb_set to it. The results are aggregated in SELECT    LATERAL jsonb_set(individual_object, path, new_value) updated_jsonb  $$;

And that's it... :)

I hope it'll help someone with the same problem I had.

answeredDec 10, 2018 at 19:22
kubak's user avatar

Comments

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.