- MySQL - json_object/json_array,
- MS SQL Server - SELECT [...] FOR JSON
- PostgreSQL - json_build_object/json_agg
- for any RDBMS without JSON support - by normal concatenation.
Or post-processing the values selected by the query in any language.