1

So, what I need is a following json.

var plan = [{
  id: 11,
  title: 'give a title',
  actions: [
   {id: 1,
   planId: 11,
   title: 'give action name'},
   {
     id: 3,
     planId: 11,
     title: 'give another action name'
   }
  ]},
{
      id: 13,
      title: 'thirteen a title',
      actions: [
       {id: 1,
       planId: 13,
       title: 'thirteen action name'},
       {
         id: 3,
         planId: 13,
         title: 'thirteen another action name'
       }
      ]}
]

SO I have 2 tables, plan and actions. The relation between two table is Plan has many actions. Plan(id, titile) Action(id, titile, planId)

SELECT
*,
ARRAY (
    SELECT
        jsonb_build_object ('id',
            m.id,
            'title',
            m.title)
    FROM
        actions a
        INNER JOIN plan p ON p.id = a.planid
    ) AS actions
FROM
    plan

I'm not sure how can I get the related actions under each plan.

3 Answers 3

2

These queries might be what you're looking for:

Data sample

CREATE TEMPORARY TABLE plan (id INT, title TEXT);
CREATE TEMPORARY TABLE action (id INT, title TEXT, planid INT);

INSERT INTO plan VALUES (1,'plan a'),(2,'plan b');
INSERT INTO action VALUES (1,'1st action plan a',1),
                          (2,'2nd action plan a',1),
                          (3,'1st action plan b',2);

Query - multiple json records

SELECT 
  json_build_object(
    'id',p.id,'title',p.title,
    'actions',(SELECT json_agg(row_to_json(t)) 
               FROM (SELECT id,title,planid 
               FROM action WHERE planid = p.id) t)) AS myjson
FROM plan p;
                                                                        myjson                                                                     
------------------------------------------------------------------------------------------------------------------------------------------------
 {"id" : 1, "title" : "plan a", "actions" : [{"id":1,"title":"1st action plan a","planid":1}, {"id":2,"title":"2nd action plan a","planid":1}]}
 {"id" : 2, "title" : "plan b", "actions" : [{"id":3,"title":"1st action plan b","planid":2}]}
(2 Zeilen)

Query - single json record

SELECT json_agg(row_to_json(myjson)) FROM 
(SELECT 
  json_build_object(
    'id',p.id,'title',p.title,
    'actions',(SELECT json_agg(row_to_json(t)) 
               FROM (SELECT id,title,planid 
               FROM action WHERE planid = p.id) t)) as plan
FROM plan p) myjson;

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 [{"plan":{"id" : 1, "title" : "plan a", "actions" : [{"id":1,"title":"1st action plan a","planid":1}, {"id":2,"title":"2nd action plan a","planid":1}]}}, {"plan":{"id" : 2, "title" : "plan b", "actions" : [{"id":3,"title":"1st action plan b","planid":2}]}}]
(1 Zeile)
Sign up to request clarification or add additional context in comments.

2 Comments

IMO array_to_json(array_agg(...)) could be simplified just to json_agg(...)
@Abelisto you're damn right.. just edited my answer. Thanks!
1

This should do it:

SELECT
array_to_json(array_agg(row_to_json(tb_data))) AS data FROM 
(
    SELECT
    tb_plan.id,
    tb_plan.title,
    array_to_json(array_agg(row_to_json(tb_action))) AS actions
    FROM plan tb_plan
    INNER JOIN "action" tb_action ON tb_plan.id = tb_action.planid
    GROUP BY 1,2
) tb_data

Here's the documentation about the postgresql's json functions used in this query.

Comments

0

Something like this should do the job:

SELECT json_agg(data)
FROM
  (SELECT p.id,
          p.title,
          json_agg(a) FILTER (WHERE a.plan_id IS NOT NULL) AS actions
   FROM PLAN p
   LEFT JOIN action a ON a.plan_id = p.id
   GROUP BY p.id,
            p.title) data;
[
  {
    "id": 1,
    "title": "test plan 1",
    "actions": [
      {
        "id": 1,
        "title": "test action 1",
        "plan_id": 1
      },
      {
        "id": 2,
        "title": "test action 2",
        "plan_id": 1
      }
    ]
  },
  {
    "id": 2,
    "title": "test plan 2",
    "actions": [
      {
        "id": 3,
        "title": "test action 3",
        "plan_id": 2
      },
      {
        "id": 4,
        "title": "test action 4",
        "plan_id": 2
      }
    ]
  }
]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.