Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save rusllonrails/cecdcf8cedf6e4034000 to your computer and use it in GitHub Desktop.
Save rusllonrails/cecdcf8cedf6e4034000 to your computer and use it in GitHub Desktop.
POSTGRESQL | JSONB LOOP ARRAYS EXAMPLE 2
CREATE OR REPLACE FUNCTION test1(in_array jsonb)
RETURNS text[] AS $$
DECLARE
js jsonb := in_array;
i jsonb;
result_ids TEXT[];
uuid text;
state text;
version text;
category_id text;
expires_on date;
issue_date date;
updated_at date;
BEGIN
raise notice ' ';
raise notice 'js: %', js;
raise notice ' ';
raise notice 'jsonb_array_length(js): %', jsonb_array_length(js);
FOR i IN SELECT * FROM jsonb_array_elements(js)
LOOP
uuid := i->>'uuid';
state := i->>'state';
version := i->>'version';
expires_on := i->>'expires_on';
issue_date := i->>'issue_date';
updated_at := i->>'updated_at';
category_id := i->>'category_id';
RAISE NOTICE '-----------------------------------';
RAISE NOTICE 'uuid %', uuid;
RAISE NOTICE 'state %', state;
RAISE NOTICE 'version %', version;
RAISE NOTICE 'expires_on %', expires_on;
RAISE NOTICE 'issue_date %', issue_date;
RAISE NOTICE 'updated_at %', updated_at;
RAISE NOTICE 'category_id %', category_id;
RAISE NOTICE '-----------------------------------';
IF state = 'regular' THEN
result_ids := array_append(result_ids, uuid);
END IF;
END LOOP;
RAISE NOTICE '-----------------------------------';
RAISE NOTICE 'result_ids %', result_ids;
RAISE NOTICE '-----------------------------------';
RETURN result_ids;
END;
$$ LANGUAGE plpgsql;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment