Search code examples
postgresqljsonb

How to convert element to array in jsonb [Postresql]


I need to convert some element in jsonb column to array

What I have:

{"a": {
          "b": "2022-11-03",
          "c": "321321",
          "d": "213321"
     }
}

What I need: `

{"a": [
        {
          "b": "2022-11-03",
          "c": "321321",
          "d": "213321"
         }
      ]
}

Solution

  • you can use jsonb_set() for this:

    SELECT jsonb_set(the_column, '{a}', jsonb_build_array(the_column -> 'a'))
    FROM the_table