Skip to content

Instantly share code, notes, and snippets.

@abn
Created September 14, 2020 21:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abn/85b4613be44f87e105d8dcea06d7b89d to your computer and use it in GitHub Desktop.
Save abn/85b4613be44f87e105d8dcea06d7b89d to your computer and use it in GitHub Desktop.
Implicit casting from jsonb array to psql text array
-- https://stackoverflow.com/a/45244285
CREATE OR REPLACE FUNCTION public.jsonb_array_to_text_array(
JSONB
) RETURNS TEXT[] AS
$f$
SELECT array_agg(x::TEXT) FROM jsonb_array_elements($1) t(x);
$f$
LANGUAGE sql
IMMUTABLE;
CREATE CAST (JSONB AS TEXT[]) WITH FUNCTION public.jsonb_array_to_text_array(JSONB) AS IMPLICIT;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment