I have a simple json data and I want to add another key:value to it.
select elem, position
from jsonb_all_testing,jsonb_array_elements(jsonb_col->'UserProfile') with ordinality arr(elem,position)
where col1=2
The output is
{"ProfileName": "WalletCustomer"} 1
{"UserBand": "MEDIUM", "ProfileName": "PMAdmin"} 2
{"ProfileName": "CorporateAdmin"} 3
Now I would like to add another KEY:VALUE to element 2. After the addition it should look like
{"UserBand": "MEDIUM", "ProfileName": "PMAdmin","OneMoreKey":"NewValue"}
but I want to pass the position "2" (actually 2-1) dynamically as I would not know where to add.
So I would like to be able to do something like this
with t as (select elem, position
from jsonb_all_testing,jsonb_array_elements(jsonb_col->'UserProfile') with ordinality arr(elem,position)
where col1=2 and elem->>'ProfileName'='PMAdmin')
update jsonb_all_testing
set jsonb_col=jsonb_set(jsonb_col,'{UserProfile,**t.position-1,**""OneMoreKey"}','"NewValue"')
where col1=2;
But this is not working. I have set the array position explicitly as numeral 1.
How can I achieve this and set the value dynamically?