In jsonb-Objekt mit jsonb_set Array setzen

Hi

Ich versuche in einer Funktion ein jsonb-Objekt zu füllen und zurückzugeben.

Ich hatte Erfolg z.B. mit


DECLARE
  result jsonb;
  names text[];

result = jsonb_build_object();
result = jsonb_set(result ::jsonb, '{resultCode}','"OK"');
return result ;

aber es scheitert

	select
		names
	into
		names
	from
		test
	where
		id = 1
	;

result = jsonb_set(result ::jsonb, '{names}', to_jsonb(names));

Die letzte Zeile scheint falsch. Das SELECT auf die Funktion liefert NULL.

Was mache ich falsch?

Dies funktioniert ebbenfalls
result = jsonb_set(result::jsonb, ‘{names}’, to_jsonb(ARRAY[‘a’, ‘b’, ‘c’]));