title: Unexpected token o in JSON at position 1
body: <p>I keep getting this error in this block of code below:</p>
  
  <pre><code>function openWebsocket(url) {
      var ws;
      ws = $websocket(url);
      ws.onOpen(function(event) {
          console.log(' Websocket connection established:', event);
      });
      ws.onMessage(function(message) {
          var userObj   = UserFactory.getUserObject();
          var settings  = userObj.alert_settings;
  
          // The JSON parsing...
          var parsedMsg = JSON.parse(message.data);
          var alert     = JSON.parse(parsedMsg);
          var date      = new Date(parseFloat(alert.start_epoch+'000'));
          alert.hour    = date.getHours() +':'+date.getMinutes();
          alert.percent_change = Math.round(alert.percent_change);
  
          var shouldPush = main_alert_filter(settings, alert);
          updateFeed(alerts, shouldPush, alert);
      });
  }
  </code></pre>
  
  <p>I've looked at both <a href="https://stackoverflow.com/questions/15617164/parsing-json-giving-unexpected-token-o-error">Parsing JSON giving &quot;unexpected token o&quot; error</a> and <a href="https://stackoverflow.com/questions/8081701/i-keep-getting-uncaught-syntaxerror-unexpected-token-o">I keep getting &quot;Uncaught SyntaxError: Unexpected token o&quot;</a></p>
  
  <p>However neither answer helped. Because when I first run JSON.parse(message.data) I get a string back not an Object. So thus I have to run JSON.parse again to finally get a real object back.</p>
  
  <p><a href="https://i.stack.imgur.com/ef2gJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ef2gJ.png" alt="enter image description here"></a></p>
  
  <p><strong>This is what <code>message.data</code> looks like:</strong></p>
  
  <p><code>
  "
  "{\"term\": \"\\\"nike\\\"\", \"percent_change\": 125, \"hour\": \"10:9\", \"term_id\": 2890413, \"start_epoch\": 1420474140, \"term_trend_id\": 793950, \"end_epoch\": 1420477740, \"formatted_date_difference\": \"January  5, 2015\", \"tickers\": [\"NKE\", \"$PUM\", \"ADDYY\", \"LULU\", \"UA\", \"HIBB\"], \"twitter_preview\": \"\", \"type\": \"spike\", \"approved\": 1, \"search_preview\": [\"\"]}"
  "
  </code></p>
  
  <p><strong>Now after the first parsing <code>parsedMsg</code> is a string that looks like this:</strong></p>
  
  <p><code>{"term": "minimum wage +increase", "percent_change": 729, "hour": "9:14", "term_id": 2522115, "start_epoch": 1447168440, "term_trend_id": 657898, "end_epoch": 1447175700, "formatted_date_difference": "November 10, 2015", "tickers": ["$JAB", "$SLCY", "AAL", "AAPL", "ABCD", "ABTL", "ADDYY", "ADM", "AEO", "AFCO", "AHC"......</code></p>
  
  <p><strong>Finally I need an actual object, so I have to run JSON.parse again to get this:</strong></p>
  
  <p><code>Object {term: "minimum wage +increase", percent_change: 729, hour: "9:14", term_id: 2522115, start_epoch: 1447168440…}</code></p>
  
  <p>Another thing to note, I never get that error when I'm stepping through in Chrome. It only happens when I don't have the breakpoint set. Could this be a race condition type issue? Like it tries to JSON.parse something that isn't ready to be parsed?</p>
  
  <hr>
  
  <p>UPDATE</p>
  
  <p>Ok so sometimes the JSON is invalid apparently and sometimes not, so far I'm doing good without errors with the following snippet, thoughts?</p>
  
  <pre><code>if (typeof alert === 'object') {
      // do nothing...
  } else {
      var alert = JSON.parse(alert);
  }
  </code></pre>
  
  <p>Most of the time the <code>alert</code> result of <code>JSON.parse(message.data)</code> is a <code>string</code> so I need the other check to double parse it.</p>