title: How to return a json array from a query?
body: <p>In a postgres 10 database, there are three tables: </p>
  
  <p>main: </p>
  
  <pre><code>id | name
  -----------
  1  | first
  2  | second
  3  | third
  …
  </code></pre>
  
  <p>substances</p>
  
  <pre><code>id  | name
  ----------------------
  1   | gold
  2   | silver
  3   | aluminum
  …
  </code></pre>
  
  <p>link</p>
  
  <pre><code>id  | id_main  | id_substance
  ---------------------------------
  1   | 1        | 1
  2   | 1        | 2
  3   | 1        | 3
  4   | 2        | 1
  5   | 3        | 2
  6   | 3        | 3
  </code></pre>
  
  <p><strong>How to make query to return a json object like this?</strong></p>
  
  <pre><code>[
    {
      "name": "first",
      "substances": ["gold", "silver", "aluminum"]
    },
    {
      "name": "second",
      "substances": ["gold"]
     },
     {
      "name": "third",
      "substances": ["silver", "aluminum"]
     }
  ]
  </code></pre>