PostgreSQL - Add key to each objects of an JSONB array

前端 未结 1 1515
名媛妹妹
名媛妹妹 2021-01-26 19:17

My database contains a table which has a column with jsonb type, and I want to update a part of these data using functions/operators from postgreSQL

相关标签:
1条回答
  • 2021-01-26 19:18

    The approach with jsonb_array_elements and jsonb_set was the right idea, but somehow you nested them the wrong way round:

    UPDATE myTable SET myColumn = jsonb_set(myColumn, '{A}', (
      SELECT jsonb_agg( element || '{"myArray":[]}' )
      FROM jsonb_array_elements(myColumn -> 'A') element
    ));
    

    (online demo)

    Btw if your column already has jsonb data type, you shouldn't need any casts.

    0 讨论(0)
提交回复
热议问题