SELECT row_to_json main_json_query FROM SELECT SELECT coalesce array_a

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
SELECT row_to_json(main_json_query.*)
FROM (
SELECT (
SELECT coalesce(
array_agg(data_query.data),
CAST(ARRAY[] AS JSON[])
) AS data
FROM (
SELECT
json_build_object(
'id',
CAST(article.id AS VARCHAR),
'type',
'articles',
'attributes',
json_build_object(
'name',
article.name
),
'relationships',
json_build_object(
'comments',
json_build_object(
'data',
(
SELECT
coalesce(
array_agg(relationships.json_object),
CAST(ARRAY[] AS JSON[])
) AS coalesce_2
FROM (
SELECT json_build_object(
'id',
CAST(comment.id AS VARCHAR),
'type',
'comments'
) AS json_object
FROM comment
WHERE article.id = comment.article_id
) AS relationships
)
)
)
) AS data
FROM article
) AS data_query
) AS data
) AS main_json_query