title: How to create nested JSON objects dynamically?
body: <p>I want to create a JSON object <code>words</code> which further contains a list of
   objects <code>s</code> and <code>e</code> with different values as follows:</p>
  
  <pre><code>words = {
      "a1": { s: 1.5, e: 2 },
      "a2": { s: 2.1, e: 2.4 },
      "a3": { s: 2.5, e: 3 },
      "a4": { s: 3.5, e: 3.8 },
      "a5": { s: 3.9, e: 4.5 },
      "a6": { s: 4.8, e: 5.2 },
      "a7": { s: 5.3, e: 5.3 },
      "a8": { s: 6.5, e: 7 },
      "a9": { s: 7.2, e: 8 },
      "a10": { s: 8.1, e: 8.4 }
  };
  </code></pre>
  
  <p>I need to add these values using a <code>for</code> loop. I have applied the
  following solution for this:</p>
  
  <pre><code>var words = {};
  var xyz = [1.5,2,2.1,2.4,2.5,3,3.5,3.8,3.9,4.5,4.8,5.2,5.3,5.3,6.5,7,7.2,8];
  words = '{';
  for (var i = 0; i &lt;= 10; i++) {
      if (i == 10)
          var obj = '\"a' + i + '\"'+': { s:' + xyz[i] + ', e:' + xyz[i + 1] + '}';
      else
          var obj = '\"a' + i + '\": { s:' + xyz[i] + ', e:' + xyz[i + 1] + '},';
  
      words = words + obj;
  }
  words = words + '}';
  
  $.parseJSON(words);
  </code></pre>
  
  <p>But this generates an error of:</p>
  
  <blockquote>
    <p>Uncaught SyntaxError: Unexpected token s**</p>
  </blockquote>
  
  <p>I am not able to create the object due to this. </p>