title: Complicated JSON with Cakephp 3
body: <p>I am working on a project which involves passing data "profiles" via JSON to a web application.  I am using Cakephp 3.0 and am very new to it.  I store the profiles in a mysql database and can easily query for the data and put it into a basic JSON format with each row being a separate value in the JSON:</p>
  
  <p>Controller.php:</p>
  
  <pre><code>....
  public function getProfileData()
      {
          $uid = $this-&gt;Auth-&gt;user('id');
          $this-&gt;loadComponent('RequestHandler');
          $this-&gt;set('profile', $this-&gt;MapDisplay-&gt;find(
              'all',
              ['conditions' =&gt; 
                  ['MapDisplay.user_id =' =&gt; $uid]
              ]
          )
          );
          $this-&gt;set('_serialize', ['profile']);
      }
  ....
  </code></pre>
  
  <p>get_profile_data.ctp:</p>
  
  <pre><code>&lt;?= json_encode($profile); ?&gt;
  </code></pre>
  
  <p>Which returns something like this:</p>
  
  <pre><code>{
  "profile": [
      {
          "alert_id": 1,
          "alert_name": "Test",
          "user_id": 85,
          "initialized_time": "2017-03-24T00:00:00",
          "forecasted_time": "2017-03-24T00:10:00",
          "minimum_dbz_forecast": 0,
          "maximum_dbz_forecast": 10,
          "average_dbz_forecast": 5,
          "confidence_in_forecast": 0.99,
          "alert_lat": 44.3876,
          "alert_lon": -68.2039
      },
      {
          "alert_id": 1,
          "alert_name": "Test",
          "user_id": 85,
          "initialized_time": "2017-03-24T00:00:00",
          "forecasted_time": "2017-03-24T00:20:00",
          "minimum_dbz_forecast": 5,
          "maximum_dbz_forecast": 15,
          "average_dbz_forecast": 10,
          "confidence_in_forecast": 0.99,
          "alert_lat": 44.3876,
          "alert_lon": -68.2039
      },
      {
          "alert_id": 2,
          "alert_name": "Test2",
          "user_id": 85,
          "initialized_time": "2017-03-24T00:00:00",
          "forecasted_time": "2017-03-24T00:10:00",
          "minimum_dbz_forecast": 10,
          "maximum_dbz_forecast": 20,
          "average_dbz_forecast": 15,
          "confidence_in_forecast": 0.99,
          "alert_lat": 44.5876,
          "alert_lon": -68.1039
      },
      {
          "alert_id": 2,
          "alert_name": "Test2",
          "user_id": 85,
          "initialized_time": "2017-03-24T00:00:00",
          "forecasted_time": "2017-03-24T00:20:00",
          "minimum_dbz_forecast": 15,
          "maximum_dbz_forecast": 25,
          "average_dbz_forecast": 35,
          "confidence_in_forecast": 0.99,
          "alert_lat": 44.5876,
          "alert_lon": -68.1039
  ]
  }
  </code></pre>
  
  <p>I am hoping to A) Easily call individual profiles instead of searching for unique profile ids and B) Only have to load one JSON file to get all profile contents.  An output of something like this would be more ideal:</p>
  
  <pre><code> {
  "profile": [
      {
          "alert_id": 1,
          "alert_name": "Test",
          "initialized_time":"2017-03-24T00:00:00",
          "alert_lat": 44.3876,
          "alert_lon": -68.2039,
          "profile_data": [
              {
                  "forecasted_time": "2017-03-24T00:10:00",
                  "minimum_dbz_forecast": 0,
                  "maximum_dbz_forecast": 10,
                  "average_dbz_forecast": 5,
                  "confidence_in_forecast": 0.99
              },
              {
                  "forecasted_time": "2017-03-24T00:20:00",
                  "minimum_dbz_forecast": 5,
                  "maximum_dbz_forecast": 15,
                  "average_dbz_forecast": 10,
                  "confidence_in_forecast": 0.99
              }
          ]
      },
      {
          "alert_id": 2,
          "alert_name": "Test2",
          "initialized_time": "2017-03-24T00:00:00",
          "alert_lat": 44.5876,
          "alert_lon": -68.1039,
          "profile_data": [
              {
                  "forecasted_time": "2017-03-24T00:10:00",
                  "minimum_dbz_forecast": 10,
                  "maximum_dbz_forecast": 20,
                  "average_dbz_forecast": 15,
                  "confidence_in_forecast": 0.99
              },
              {
                  "forecasted_time": "2017-03-24T00:20:00",
                  "minimum_dbz_forecast": 15,
                  "maximum_dbz_forecast": 25,
                  "average_dbz_forecast": 35,
                  "confidence_in_forecast": 0.99
              }
          ]
      }
  ]
  }
  </code></pre>
  
  <p>How would I go about querying my database and populating this JSON structure?  Are there any Cakephp tools that help do this?  Does reframing the JSON into this structure seem to make sense?</p>
  
  <p>Thanks in advance!</p>