Skip to content

Instantly share code, notes, and snippets.

@rusllonrails
Created August 9, 2015 14:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rusllonrails/3e0c87514f1c2f9a19b0 to your computer and use it in GitHub Desktop.
Save rusllonrails/3e0c87514f1c2f9a19b0 to your computer and use it in GitHub Desktop.
POSTGRESQL | LOOP JSONB ARRAY
CREATE OR REPLACE FUNCTION test1(in_array jsonb)
RETURNS jsonb AS $$
DECLARE
js jsonb := in_array;
i jsonb;
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
RAISE NOTICE '-----------------------------------';
RAISE NOTICE 'uuid %', i->>'uuid';
RAISE NOTICE 'state %', i->>'state';
RAISE NOTICE 'version %', i->>'version';
RAISE NOTICE 'expires_on %', i->>'expires_on';
RAISE NOTICE 'issue_date %', i->>'issue_date';
RAISE NOTICE 'updated_at %', i->>'updated_at';
RAISE NOTICE 'category_id %', i->>'category_id';
RAISE NOTICE '-----------------------------------';
END LOOP;
RETURN js;
END;
$$ LANGUAGE plpgsql;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment