Skip to content

Instantly share code, notes, and snippets.

@arduanov
Created January 21, 2016 14:17
Show Gist options
  • Select an option

  • Save arduanov/2ebcbc82bdacb041da6e to your computer and use it in GitHub Desktop.

Select an option

Save arduanov/2ebcbc82bdacb041da6e to your computer and use it in GitHub Desktop.
Update: In PostgreSQL 9.4 this improves a lot with the introduction of to_json, json_build_object, json_object and json_build_array, though it's verbose due to the need to name all the fields explicitly:
select
json_build_object(
'id', u.id,
'name', u.name,
'email', u.email,
'user_role_id', u.user_role_id,
'user_role', json_build_object(
'id', ur.id,
'name', ur.name,
'description', ur.description,
'duty_id', ur.duty_id,
'duty', json_build_object(
'id', d.id,
'name', d.name
)
)
)
from users u
inner join user_roles ur on ur.id = u.user_role_id
inner join role_duties d on d.id = ur.duty_id;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment