Re: [GENERAL] JSON to INT[] or other custom type - Mailing list pgsql-general

From David G. Johnston
Subject Re: [GENERAL] JSON to INT[] or other custom type
Date
Msg-id CAKFQuwZgMMkA8+soo=viScJaPc4B90-ShUBnk=ONPSj149JN5w@mail.gmail.com
Whole thread Raw
In response to [GENERAL] JSON to INT[] or other custom type  (Rory Campbell-Lange <rory@campbell-lange.net>)
List pgsql-general
On Sun, Jun 11, 2017 at 2:35 PM, Rory Campbell-Lange <rory@campbell-lange.net> wrote:
I'm hoping, in the plpgsql function, to unfurl the supplied json into a
custom type or at least an array of ints, and I can't work out how to do
that.

​You will be unable to cast the array itself.  You must use json functions to unfurl the json into a result set with each row containing a single value.  You can then explicitly cast that value to integer.  If the casting doesn't fail you now have column of integers that can be "array_agg(value_as_integer)​" to construct an SQL array of type integer[] (or whatever you decide to cast it to).

You can write you own pl/pgsql function to do these operations.

As your example includes a two-dimensional array you probably will need to json_array_elements_text twice, then array_agg twice, to get a similar structure in the eventual SQL array.

Hopefully that moves you in a useful direction.

David J.

pgsql-general by date:

Previous
From: Alvaro Herrera
Date:
Subject: Re: [GENERAL] Unsubscription
Next
From: "David G. Johnston"
Date:
Subject: Re: [GENERAL] Inconsistent performance with LIKE and bind variable onlong-lived connection