title: How to push JSON Objects and create a nested one in AngularJs
body: <p>I end up with a frontend task on my desk yesterday and since our frontend dev is on annual leave I need some help.</p>
  
  <p>I have this piece of AngularJs script which is part of my angular controller. I am trying to post some data to the server which I fetch from a multi-step wizard form. On the server side I have already the implementation (I am a backend dev) so nothing scary there but I need to tweak somehow the JSON object before I try to POST so I can bind that nicely with my Spring boot rest controller. </p>
  
  <p><strong>That's the piece of code.</strong> </p>
  
  <pre><code>//These are my guys which I am trying to push    
  $scope.department = {};
  $scope.user1 = {};
  $scope.user2 = {};
  
  
  //Some POST method
  $scope.save = function () {
  
      var data = [{}];
      //here I do the push but it looks like I've no idea what I am doing
      data.push($scope.department);
      data.push($scope.user1);
      data.push($scope.user2);
  
      var config = {
              headers : {
                  'Content-Type': 'application/json'
              }
          }
  
          console.log(JSON.stringify(data));
  
          $http.post('http://localhost:8080/api/', data, config)
          .success(function (data, status, headers, config) {
              $scope.PostDataResponse = data;
          })
          .error(function (data, status, header, config) {
              console.log(data);
          });
      };
  </code></pre>
  
  <p><strong>This is the JSON output of what I have achieved with the above script</strong></p>
  
  <pre><code>[
      {
          "department": "foo",
          "code": 1234
      },
      {
          "firstName": "Megan",
          "lastName": "Fox"
      },
      {
          "firstName": "Margot",
          "lastName": "Robbie"
      }
  ]
  </code></pre>
  
  <p><strong>This is the JSON output I am looking for</strong></p>
  
  <pre><code>{
      "department": "foo",
      "code": 1234,
  
      "user1": {
          "firstName": "Megan",
          "lastName": "Fox"
      },
  
      "user2": {
          "firstName": "Margot",
          "lastName": "Robbie"
      }
  }
  </code></pre>
  
  <p>Thank you!!!</p>