Skip to content Skip to sidebar Skip to footer

Unexpected Token O In Json At Position 1

I keep getting this error in this block of code below: function openWebsocket(url) { var ws; ws = $websocket(url); ws.onOpen(function(event) { console.log(' Web

Solution 1:

Why would you parse your json second time, its already been parsed in the first attempt.

Have a look at the snippet

var obj = "{\"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\"]}";
$(function(){
  var data = JSON.parse(obj);
  alert(typeof data);
  console.log(data.tickers[0] +" -> an item in `tickers` array");
  console.log(data.tickers);
});
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Solution 2:

The JSON string you specified with message.data is not a well formed JSON parsed as String. It might be because the server is sending you a multi-part message during/after establishing the connection.

I suggest you print the message object received in OnMessage function and analyze if they are fully formed valid JSON Strings.

Solution 3:

It looks like Your message.data is incomplete.

Take a look on the library docs You are using, maybe you should collect the data until it's end? Maybe there is some onEnd method?

Post a Comment for "Unexpected Token O In Json At Position 1"