title: JSON parsing: Unexpected Token error
body: <p>Im trying to parse a string to JSON and I'm getting an unexpected token error. </p>
  
  <p>I am checking validity using <a href="http://json.parser.online.fr/" rel="nofollow">http://json.parser.online.fr/</a> which comes up with no parse errors, but still says the eval fails due to an unexpected token. If you paste the JSON from below in to that website you can see that it finds an error, but doesn't specify what token is causing it.</p>
  
  <p>Heres what I'm trying to parse.</p>
  
  <pre><code>{
      "Polish": {
          "Rent": [
              {
                  "english": "a",
                  "audioUrl": "b",
                  "alternate": "c"
              },
              {
                  "english": "d",
                  "audioUrl": "e",
                  "alternate": "f"
              }
          ]
      }
  }
  </code></pre>
  
  <p>Am I missing something obvious?</p>
  
  <p><br>
  <strong>EDIT</strong> <br>
  There is an unprintable character inbetween the : and [ after the "Rent" key.</p>
  
  <p>I am doing some replace() calls on the string prior to the parse attempt which are likely creating the problem.</p>
  
  <p>prior to the parse that particular line is </p>
  
  <pre><code>"Rent":"[
  </code></pre>
  
  <p>I want to remove the doublequote between the : and [ sybmols.</p>
  
  <p>So I am using:</p>
  
  <pre><code>var reg = new RegExp('":"', 'g');
  var newStr = originalStr.replace(reg, '":');
  </code></pre>
  
  <p>I don't know why the above is causing the unprintable character though.</p>
  
  <p><br>
  <strong>EDIT2</strong><br>
  I did a quick check removing the Above replace() call pasted it into the validator, manually removed the doublequotes I was using replace() on, and the unreadable characters are still there. So the error is present in the original string. So more code :|</p>
  
  <p>The string is being returned from an ajax call to a php script residing on a server. The PHP script is reading a directory on the server and populating nested associative array to produce the string which is sent back to the JS side, which edits and parses it (shown above).</p>
  
  <p>Within the directories are JSON files, which I'm inserting the contents of into this nested array structure to complete the JSON hierarchy. </p>
  
  <p>The unreadable characters were </p>
  
  <blockquote>
    <p>ef bb bf </p>
  </blockquote>
  
  <p>Which I googled and found to be the <a href="http://en.wikipedia.org/wiki/Byte_Order_Mark" rel="nofollow">Byte Order Mark</a> of the string representing the file contents.</p>
  
  <p>So heres the PHP Code which reads the directories and JSON files creating a nested array structure to be JSON_encode()d and sent back to the JS</p>
  
  <pre><code>if ($langHandle = opendir($langDir)) {
  while (false !== ($langEntry = readdir($langHandle))) {
      $currentLangDir = $langDir . "/" . $langEntry;
      if (is_dir($currentLangDir) &amp;&amp; $langEntry != '.' &amp;&amp; $langEntry != '..') {
          $currentLang = array();
          if ($currentLangHandle = opendir($currentLangDir)) {
              while (false !== ($catEntry = readdir($currentLangHandle))) {
                  $currentCatFile = $currentLangDir . "/" . $catEntry;
                  if(is_file($currentCatFile) &amp;&amp; $catEntry != '.' &amp;&amp; $catEntry != '..') {
                      $currentCat = file_get_contents($currentCatFile);
                      $currentLang[removeFileExtension($catEntry)] = $currentCat;
                  }
              }
          }
          $langArray[$langEntry] = $currentLang;
      }
  }
  </code></pre>
  
  <p>What can I do to fix these unwanted characters, a quick search on removing the BOM chars suggests it is a bad thing to do. </p>