sql >> Base de Datos >  >> RDS >> PostgreSQL

convertir el valor de la columna jsonb en varias columnas en PostgreSQL

Es simple en 9.4 (funciones LATERAL join y jsonb usadas):

    postgres=# SELECT * 
                  FROM sample, jsonb_to_record(jsonb, true) AS x(a text, b text);
     id |            jsonb             |    date     |  a   |   b    
    ----+------------------------------+-------------+------+--------
      1 | {"a": "a", "b": "b"}         | 2014-01-06  | a    | b
      2 | {"a": "a", "b": "b"}         | 2014-01-06  | a    | b
      3 | {"a": "Ahoj", "b": "Nazdar"} | 2014-01-06  | Ahoj | Nazdar
    (3 rows)

resultado exacto:

postgres=# SELECT id, a, b, date 
               FROM sample, jsonb_to_record(jsonb, true) AS x(a text, b text);
 id |  a   |   b    |    date    
----+------+--------+------------
  1 | a    | b      | 2014-01-06
  2 | a    | b      | 2014-01-06
  3 | Ahoj | Nazdar | 2014-01-06
(3 rows)