Javascript Recursion for creating a JSON object
<p>needing some advice on how to do this properly recursively.</p>

<p>Basically what I'm doing, is entering in a bunch of text and it returns it as JSON.</p>

<p>For example:</p>

<p>The text:</p>

<pre><code>q
b
name:rawr
</code></pre>

<p>Returns:</p>

<pre><code>[
  "q",
  "b",
  {
    "name": "rawr"
  }
]
</code></pre>

<p>And the following input:</p>

<pre><code>q
b
name:rawr:awesome
</code></pre>

<p>Would return (output format is not important):</p>

<pre><code>[
  "q",
  "b",
  {
    "name": {
        "rawr": "awesome"
    }
  }
]
</code></pre>

<hr>

<p>How can I modify the following code to allow a recursive way to have objects in objects.</p>

<pre><code>var jsonify = function(input){
  var listItems = input, myArray = [], end = [], i, item;

  var items = listItems.split('\r\n');

  // Loop through all the items
  for(i = 0; i &lt; items.length; i++){

    item = items[i].split(':');

    // If there is a value, then split it to create an object
    if(item[1] !== undefined){
      var obj = {};
      obj[item[0]] = item[1];  
      end.push(obj);
    }
    else{
      end.push(item[0]);
    }
  }

  // return the results
  return end;
};
</code></pre>
