4

I am expecting this JSON for my jQuery code:

{
  "projects": [
    {
      "id": "1",
      "project_name": "Carmichael House",
      "parent_id": "0",
      "children": [
        {
          "id": "2",
          "project_name": "Carmichael Kitchen",
          "parent_id": "1"
        },
        {
          "id": "3",
          "project_name": "Carmichael Bathroom",
          "parent_id": "1"
        }
      ]
    },
    {
      "id": "2",
      "project_name": "Dowd Apartment",
      "parent_id": "0",
      "children": [
        {
          "id": "4",
          "project_name": "Dowd Kitchen",
          "parent_id": "2"
        }
      ]
    }
  ]
}

This data will come from MySql table tbl_projects:

id
project_name
parent_id

What should the SQL SELECT query be, so that it will output 1 flat table, that can easily be converted into JSON (in PHP or JavaScript/jQuery)?

Am I even approaching this the right way?

2
  • Which version of MySQL? It matters because only later versions support recursive Common Table Expressions. Commented Mar 17, 2019 at 21:27
  • MySql version 5.7 Commented Mar 17, 2019 at 21:53

1 Answer 1

7

You can generate JSON content directly from MySQL. Here is a solution that works with MySQL 5.7 or higher.

As a starter, coonsider function JSON_OBJECT(), that generates a JSON object for each record in the table:

SELECT 
    p.*, 
    JSON_OBJECT('id', id, 'project_name', project_name, 'parent_id', parent_id) js
FROM tbl_projects p;

Given your sample data, this returns:

| id  | project_name        | parent_id | js                                                               |
| --- | ------------------- | --------- | ---------------------------------------------------------------- |
| 1   | Carmichael House    | 0         | {"id": 1, "parent_id": 0, "project_name": "Carmichael House"}    |
| 2   | Carmichael Kitchen  | 1         | {"id": 2, "parent_id": 1, "project_name": "Carmichael Kitchen"}  |
| 3   | Carmichael Bathroom | 1         | {"id": 3, "parent_id": 1, "project_name": "Carmichael Bathroom"} |
| 4   | Dowd Apartment      | 0         | {"id": 4, "parent_id": 0, "project_name": "Dowd Apartment"}      |
| 5   | Dowd Kitchen        | 4         | {"id": 5, "parent_id": 4, "project_name": "Dowd Kitchen"}        |

To generate your expected output, we will self-JOIN the table to find children records, and use aggregate function JSON_ARRAYAGG() to generate the inner JSON array. An additional level of aggregation stuffs everything into a single object. As showned in your sample data, I assumed that root projects have parent_id = 0 and that there is only one level of hierarchy:

SELECT JSON_OBJECT('projects', JSON_ARRAYAGG(js)) results
FROM (
    SELECT JSON_OBJECT(
        'id', p.id, 
        'project_name', p.project_name, 
        'parent_id', p.parent_id,
        'children', JSON_ARRAYAGG(
            JSON_OBJECT(
                'id', p1.id, 
                'project_name', p1.project_name, 
                'parent_id', p1.parent_id
            )
        )
    ) js
    FROM tbl_projects p
    LEFT JOIN tbl_projects p1 ON p.id = p1.parent_id
    WHERE p.parent_id = 0
    GROUP BY p.id, p.project_name, p.parent_id
) x

Yields:

| results                                                                                                                                                                                                                                                                                                                                                              |
| -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| {"projects": [{"id": 1, "children": [{"id": 2, "parent_id": 1, "project_name": "Carmichael Kitchen"}, {"id": 3, "parent_id": 1, "project_name": "Carmichael Bathroom"}], "parent_id": 0, "project_name": "Carmichael House"}, {"id": 4, "children": [{"id": 5, "parent_id": 4, "project_name": "Dowd Kitchen"}], "parent_id": 0, "project_name": "Dowd Apartment"}]} |

Demo on DB Fiddle

Sign up to request clarification or add additional context in comments.

2 Comments

WOW. You are equally fantastic as JSON_OBJECT() is.
Welcome Rollor... Such feedback sounds fantastic to my ears!

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.