Coalesce jsonArrayAgg to empty array in jOOQ
Asked Answered
I

1

1

What is the equivalent for coalesce in the new jOOQ 3.14 SQL/JSON supporting version (in PostgreSQL)?

select coalesce(json_agg(t.*), '[]'::json)
from (select 'test' as mycol where 1 = 2) t;

The following unfortunately fails with the error "COALESCE types json and uuid[] cannot be matched".

coalesce(jsonArrayAgg(mycol), emptyArray<type>())
Isoniazid answered 22/12, 2020 at 14:58 Comment(0)
F
1

JSON.json() or JSONB.jsonb(), e.g.:

coalesce(jsonArrayAgg(mycol), inline(json("[]")))
coalesce(jsonbArrayAgg(mycol), inline(jsonb("[]")))

But you can also use the jsonArray() or jsonbArray() constructors:

coalesce(jsonArrayAgg(mycol), jsonArray())
coalesce(jsonbArrayAgg(mycol), jsonbArray())
Feud answered 22/12, 2020 at 15:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.