title: Convert a string to a valid json object
body: <p>I am trying to return a valid JSON object from a string which contains the values from a form, however, using JavaScript JSON.parse returns an array of char values and not a JSON object.  Below is my code:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
      &lt;head&gt;
      &lt;title&gt;Select Browser Options&lt;/title&gt;
      &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
      &lt;script src='https://code.jquery.com/jquery-2.0.3.js'&gt;&lt;/script&gt;
      &lt;/head&gt;
      &lt;body style="width: 640px;height: 480px;"&gt;
      &lt;div id="hiddenX" style="display:none;"&gt;&lt;/div&gt;
      &lt;div id="hiddenY" style="display:none;"&gt;&lt;/div&gt;
      &lt;div id="searchPrefs"&gt;
          &lt;form id="searchPreferences"&gt;
          &lt;div id="searchWrapper"&gt;
              &lt;input class="id" type="text" name="id" value="googleSearch"&gt;
              &lt;input class="label" type="text" name="label" value="Google Search"&gt;
              &lt;input class="iconURL" type="text" name="iconURL" value="https://www.google.com/favicon.ico"&gt;
              &lt;input class="tabURL" type="text" name="tabURL" value="https://www.google.com/search?q="&gt;
              &lt;br&gt;&lt;/br&gt;
              &lt;input class="id" type="text" name="id" value="googleMapSearch"&gt;
              &lt;input class="label" type="text" name="label" value="Google Maps"&gt;
              &lt;input class="iconURL" type="text" name="iconURL" value="https://maps.gstatic.com/favicon3.ico"&gt;
              &lt;input class="tabURL" type="text" name="tabURL" value="https://www.google.com/maps/preview/search/"&gt;
              &lt;br&gt;&lt;/br&gt;
              &lt;input class="id" type="text" name="id" value="ddgSearch"&gt;
              &lt;input class="label" type="text" name="label" value="Duck Duck Go Search"&gt;
              &lt;input class="iconURL" type="text" name="iconURL" value="https://duckduckgo.com/favicon.ico"&gt;
              &lt;input class="tabURL" type="text" name="tabURL" value="https://duckduckgo.com/?q="&gt;
              &lt;br&gt;&lt;/br&gt;
              &lt;input class="id" type="text" name="id" value="imageSearch"&gt;
              &lt;input class="label" type="text" name="label" value="Google Image Search"&gt;
              &lt;input class="iconURL" type="text" name="iconURL" value="data:image/gif;base64,R0lGODlh…GbsuF0PHJq9WipnYJB9/UmFyIAOw=="&gt;
              &lt;input class="tabURL" type="text" name="tabURL" value="https://www.google.com/search?tbm=isch&amp;q="&gt;
              &lt;br&gt;&lt;/br&gt;
              &lt;input class="id" type="text" name="id" value="bingSearch"&gt;
              &lt;input class="label" type="text" name="label" value="Bing Search"&gt;
              &lt;input class="iconURL" type="text" name="iconURL" value="http://bing.com/favicon.ico"&gt;
              &lt;input class="tabURL" type="text" name="tabURL" value="http://www.bing.com/search?q="&gt;
              &lt;br&gt;&lt;/br&gt;
              &lt;input class="id" type="text" name="id" value="thesaurusSearch"&gt;
              &lt;input class="label" type="text" name="label" value="Thesaurus Search"&gt;
              &lt;input class="iconURL" type="text" name="iconURL" value="http://static.sfdict.com/thescloud/favicon.ico"&gt;
              &lt;input class="tabURL" type="text" name="tabURL" value="http://thesaurus.com/browse/"&gt;
              &lt;br&gt;&lt;/br&gt;
              &lt;input class="id" type="text" name="id" value="wikipediaSearch"&gt;
              &lt;input class="label" type="text" name="label" value="Wikipedia Search"&gt;
              &lt;input class="iconURL" type="text" name="iconURL" value="https://bits.wikimedia.org/favicon/wikipedia.ico"&gt;
              &lt;input class="tabURL" type="text" name="tabURL" value="https://en.wikipedia.org/w/index.php?search="&gt;
              &lt;br&gt;&lt;/br&gt;
              &lt;input class="id" type="text" name="id" value="wiktionarySearch"&gt;
              &lt;input class="label" type="text" name="label" value="Wiktionary Search"&gt;
              &lt;input class="iconURL" type="text" name="iconURL" value="https://bits.wikimedia.org/favicon/wiktionary/en.ico"&gt;
              &lt;input class="tabURL" type="text" name="tabURL" value="https://en.wiktionary.org/w/index.php?search="&gt;
              &lt;br&gt;&lt;/br&gt;
          &lt;/div&gt;
          &lt;a id="more" href="#"&gt;&lt;/a&gt;
          &lt;br&gt;&lt;/br&gt;
          &lt;input id="save" type="submit" value="save" name="save"&gt;
          &lt;/form&gt;
          &lt;div id="output"&gt;&lt;/div&gt;
          &lt;script&gt;
          $('#searchPreferences').submit(function(event) {
              var justValues = "[";
              var formJson = $('#searchPreferences').serializeArray();
              var lastKey = $("input").length - 2;
              $.each(formJson, function(i, field) {
              if (field.name === "id") {
                  justValues += '{"' + field.name + '":"' + field.value + '",';
              } else if (field.name === "tabURL" &amp;&amp; i === lastKey) {
                  justValues += '"' + field.name + '":"' + field.value + '"}';
              } else if (field.name === "tabURL") {
                  justValues += '"' + field.name + '":"' + field.value + '"},';
              } else {
                  justValues += '"' + field.name + '":"' + field.value + '",';
              }
              });
              justValues += "]";
              JSON.parse(justValues);
              for(var o in justValues){
              console.log(o+':'+justValues[o]);
              }
              console.log(justValues);
              event.preventDefault();
          });
          &lt;/script&gt;
      &lt;/div&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>and here is the console.log output:</p>
  
  <pre><code>...
  1210:?
  1211:s
  1212:e
  1213:a
  1214:r
  1215:c
  1216:h
  1217:=
  1218:"
  1219:}
  1220:]
  [{"id":"googleSearch","label":"Google Search","iconURL":"https://www.google.com/favicon.ico","tabURL":"https://www.google.com/search?q="},{"id":"googleMapSearch","label":"Google Maps","iconURL":"https://maps.gstatic.com/favicon3.ico","tabURL":"https://www.google.com/maps/preview/search/"},{"id":"ddgSearch","label":"Duck Duck Go Search","iconURL":"https://duckduckgo.com/favicon.ico","tabURL":"https://duckduckgo.com/?q="},{"id":"imageSearch","label":"Google Image Search","iconURL":"data:image/gif;base64,R0lGODlh…GbsuF0PHJq9WipnYJB9/UmFyIAOw==","tabURL":"https://www.google.com/search?tbm=isch&amp;q="},{"id":"bingSearch","label":"Bing Search","iconURL":"http://bing.com/favicon.ico","tabURL":"http://www.bing.com/search?q="},{"id":"thesaurusSearch","label":"Thesaurus Search","iconURL":"http://static.sfdict.com/thescloud/favicon.ico","tabURL":"http://thesaurus.com/browse/"},{"id":"wikipediaSearch","label":"Wikipedia Search","iconURL":"https://bits.wikimedia.org/favicon/wikipedia.ico","tabURL":"https://en.wikipedia.org/w/index.php?search="},{"id":"wiktionarySearch","label":"Wiktionary Search","iconURL":"https://bits.wikimedia.org/favicon/wiktionary/en.ico","tabURL":"https://en.wiktionary.org/w/index.php?search="}]
  </code></pre>