Trying to update all elements of a nested array in a jsonb column, but only one element is updated. My query:
update table_
set value_ = jsonb_set(value_,cte.json_path,cte.namevalue,false) FROM (
select
vals2->'ao'->'sc'->'name' as namevalue,
('{iProps,'||index1-1||',value,rules,'||index2-1||',ao,sc}')::text[] as json_path
from
table_,
jsonb_array_elements(value_->'iProps')
with ordinality arr1(vals1,index1),
jsonb_array_elements(vals1->'value'->'rules')
with ordinality arr2(vals2,index2)
) AS cte;
See demo with sample value:
db<>fiddle here
Am unable to understand why this query updates the first object in the rules array:
iProps -> value -> rules -> ao -> sc -> name = "name1"
But not the subsequent ones:
iProps -> value -> rules -> ao -> sc -> name = "name2"
iProps -> value -> rules -> ao -> sc -> name = "name3"