The difference is that json_build_array takes type "any" as input, while to_json takes "anyelement" as input.
Is there some reason to_json couldn't be switched to take "any" as input? Hacking this together seems to mostly just work:
postgres=# CREATE FUNCTION my_to_json ("any") RETURNS json LANGUAGE 'internal' AS 'to_json'; postgres=# SELECT my_to_json('foo'); my_to_json ------------ "foo" (1 row)
Is there something I'm missing?
Nikhil
Hm, good question. I am also curious as to why this happens. `json_build_array` ends up casting unknowns to text (from reading the code), which seems like a reasonable (although not completely tight) assumption. Not sure why `to_json` can't just do the same. You can always cast to text yourself, of course, but I am not familiar with the type hierarchy enough to tell why `to_json` can't deduce that as text whereas the other function can.