title: Iterating a JSON and having an infinite loop
body: <p>I am trying to iterate a JSON structure in a recursive way. I checked other forums / stackoverflow questions but I seem to have a strange problem.<br>
  So what comes from the REST service is :</p>
  
  <pre><code>{/Envelope=
      {/Envelope/Header = {},
       /Envelope/Body = 
           {/Envelope/Body/ns:GetAddressForPostCodeResponse=
               {/Envelope/Body/ns:GetAddressForPostCodeResponse/ns:maximumAddressMatches =  ? ,
                /Envelope/Body/ns:GetAddressForPostCodeResponse/ns:postalAddress = 
                   {/Envelope/Body/ns:GetAddressForPostCodeResponse/ns:postalAddress/ns:apartment =  ? ,
  ...
  </code></pre>
  
  <p>Basically I send back a Map from SpringMVC with a String key and having an other Map as value.</p>
  
  <p>The (angular) code:</p>
  
  <pre><code>$scope.getHtml = function(node, isAdd) {
      if (isAdd) {
          var html = "&lt;ul id='tree1'&gt;";
      } else {
          var html = "&lt;ul&gt;";
      }
  
      //angular.forEach(node, function(value, key) {
      for (var key in node) {
          if (node.hasOwnProperty(key)) {
              var value = node[key];
              console.log(key + "  :  " + value);
              console.log(node.hasOwnProperty(key));
              if (key != "$promise" &amp;&amp; key != "$resolved") {
                  html += "&lt;li&gt;&lt;input type='checkbox' id='" + key + "'&gt;&lt;label&gt;" + key + "&lt;/label&gt;";
                  html += $scope.getHtml(value, false);
              }
          }
      //});
      }
  
      html += "&lt;/ul&gt;";
      delete node;
      return html;
  };
  </code></pre>
  
  <p>As you can see there is a recursive call.</p>
  
  <p>And what I can see in the console:</p>
  
  <pre><code>true                   serviceDiscovery.controller.js:39
  "0  :  ?"              serviceDiscovery.controller.js:38
  true                   serviceDiscovery.controller.js:39
  "0  :  ?"              serviceDiscovery.controller.js:38
  ... (the same two rows some thousands times)
  "0  :  ?"              serviceDiscovery.controller.js:38
  true                   serviceDiscovery.controller.js:39
  "Error: too much recursion
  $scope.getHtml/&lt;@http://localhost:8080/management-app-1.0.0-SNAPSHOT/resources/js/serviceDiscovery.controller.js:37:21
  r@http://localhost:8080/management-app-1.0.0-SNAPSHOT/resources/js/angularjs/angular.min.js:7:288
  </code></pre>
  
  <p>So there is an infinite loop but how...? I tried the angular.forEach too.
  Any idea?</p>
  
  <p>------------- UPDATE -------------<br>
  Hmm, my mistake, sorry. I only need the recursive call if the value is a map (object) and not a String.</p>