Strange JSON interpretation (polymorphic type), how to workaround?

Viewed 2059

This legal(!) CASE construct returns a JSON datatype:

 SELECT CASE WHEN true THEN to_json(1) ELSE to_json('hello') END;

but:

ERROR: could not determine polymorphic type because input has type "unknown"

It is not "polymorphic", it is JSON.

... So, as bad workaround (lost number/string JSON representations),

 SELECT to_json(CASE WHEN true THEN 1::text ELSE 'hello' END);

Is there a better way to do this SQL-to-JSON cast?

1 Answers
Related