Обсуждение: Malformed array literal in goin from jsonb to real[]

Поиск
Список
Период
Сортировка

Malformed array literal in goin from jsonb to real[]

От
Wells Oliver
Дата:
This value exists in my jsonb column:

[[-0.7452975, -0.21457797, 0.631259], [0.66549873, -0.2969812, 0.6847727], [0.04053491, 0.9304614, 0.3641407]]

Which obviously blows up when doing (jsonb->>col)::real[] with malformed array literal.

Is there a convenient function to do this casting or do I need to do some awful string munging?

--

Re: Malformed array literal in goin from jsonb to real[]

От
Erik Wienhold
Дата:
> On 09/03/2023 16:19 CET Wells Oliver <wells.oliver@gmail.com> wrote:
>
> This value exists in my jsonb column:
>
> [[-0.7452975, -0.21457797, 0.631259], [0.66549873, -0.2969812, 0.6847727], [0.04053491, 0.9304614, 0.3641407]]
>
> Which obviously blows up when doing (jsonb->>col)::real[] with malformed
> array literal.
>
> Is there a convenient function to do this casting or do I need to do somes
> awful string munging?

Use jsonb_populate_record with a custom type:

    create type myrec as (col real[][]);

    select * from jsonb_populate_record(null::myrec, '{"col":[[0,1],[2,3]]}');

          col
    ---------------
     {{0,1},{2,3}}
    (1 row)

--
Erik



Re: Malformed array literal in goin from jsonb to real[]

От
Erik Wienhold
Дата:
> On 09/03/2023 17:05 CET Erik Wienhold <ewie@ewie.name> wrote:
>
> Use jsonb_populate_record with a custom type:
>
>     create type myrec as (col real[][]);
>     
>     select * from jsonb_populate_record(null::myrec, '{"col":[[0,1],[2,3]]}');
>     
>           col
>     ---------------
>      {{0,1},{2,3}}
>     (1 row)

I just noticed that it's also possible without a custom type:

    select * from jsonb_to_record('{"col":[[0,1],[2,3]]}') as t(col real[]);

--
Erik