title: Node.js JSON parsing error
body: <p>I am attempting to make a Facebook application with node.js, however I'm having trouble in checking signed requests. Every time I make a request, the program throws a <strong>SyntaxError: Unexpected token ILLEGAL</strong> as such:</p>
  
  <pre><code>undefined:1
  ":"721599476"}
                ^^
  SyntaxError: Unexpected token ILLEGAL
  </code></pre>
  
  <p>The culprit function is below:</p>
  
  <pre><code>function parse_signed_request(signed_request, secret) {
      encoded_data = signed_request.split('.',2);
      // decode the data
      sig = encoded_data[0];
      json = base64url.decode(encoded_data[1]);
      data = JSON.parse(json); // ERROR Occurs Here!
  
      // check algorithm - not relevant to error
      if (!data.algorithm || data.algorithm.toUpperCase() != 'HMAC-SHA256') {
          console.error('Unknown algorithm. Expected HMAC-SHA256');
          return null;
      }
  
      // check sig - not relevant to error
      expected_sig = crypto.createHmac('sha256',secret).update(encoded_data[1]).digest('base64').replace(/\+/g,'-').replace(/\//g,'_').replace('=','');
      if (sig !== expected_sig) {
          console.error('Bad signed JSON Signature!');
          return null;
      }
  
      return data;
  }
  </code></pre>
  
  <p>Just for testing, a valid signed_request would be </p>
  
  <pre><code>WGvK-mUKB_Utg0l8gSPvf6smzacp46977pTtcRx0puE.eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiIsImV4cGlyZXMiOjEyOTI4MjEyMDAsImlzc3VlZF9hdCI6MTI5MjgxNDgyMCwib2F1dGhfdG9rZW4iOiIxNTI1NDk2ODQ3NzczMDJ8Mi5ZV2NxV2k2T0k0U0h4Y2JwTWJRaDdBX18uMzYwMC4xMjkyODIxMjAwLTcyMTU5OTQ3NnxQaDRmb2t6S1IyamozQWlxVldqNXp2cTBmeFEiLCJ1c2VyIjp7ImxvY2FsZSI6ImVuX0dCIiwiY291bnRyeSI6ImF1In0sInVzZXJfaWQiOiI3MjE1OTk0NzYifQ
  </code></pre>
  
  <p>Why am I getting this error when it is valid JSON and simply using a static string of JSON will work fine, and are there any tips to fix this?</p>
  
  <p>Thanks.</p>