Update dynamic jsonb array of objects
I managed to solve this using this query:
update
   myTable 
set
   entities = 
   (
      select
         to_jsonb(array_agg (new_values)) 
      from
         (
            select
(
               case
                  when
                     arr -&gt; 'content' -&gt;&gt; 'align' = 'leftAlign' 
                  then
                     jsonb( COALESCE( jsonb_set(arr, '{content}', jsonb(arr -&gt;&gt; 'content') - 'align' || jsonb('{&quot;align&quot;: &quot;left&quot;}'), true), '{}' ) ) 
                  when
                     arr -&gt; 'content' -&gt;&gt; 'align' = 'rightAlign' 
                  then
                     jsonb( COALESCE( jsonb_set(arr, '{content}', jsonb(arr -&gt;&gt; 'content') - 'align' || jsonb('{&quot;align&quot;: &quot;right&quot;}'), true), '{}' ) ) 
                  when
                     arr -&gt; 'content' -&gt;&gt; 'align' = 'centerAlign' 
                  then
                     jsonb( COALESCE( jsonb_set(arr, '{content}', jsonb(arr -&gt;&gt; 'content') - 'align' || jsonb('{&quot;align&quot;: &quot;center&quot;}'), true), '{}' ) ) 
                  else
                     arr 
               end
) as new_values 
            from
               jsonb_array_elements( entities ) as arr 
               group by
                  arr -&gt; 'content' -&gt;&gt; 'align',
                  arr 
         )
         as new_values 
   )
;