Tuesday, May 21, 2024
 Popular · Latest · Hot · Upcoming
118
rated 0 times [  124] [ 6]  / answers: 1 / hits: 31723  / 8 Years ago, mon, july 25, 2016, 12:00:00

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(' 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);
});
}


I've looked at both Parsing JSON giving "unexpected token o" error and I keep getting "Uncaught SyntaxError: Unexpected token o"



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.



enter



This is what message.data looks like:





{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: [\]}



Now after the first parsing parsedMsg is a string that looks like this:



{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......



Finally I need an actual object, so I have to run JSON.parse again to get this:



Object {term: minimum wage +increase, percent_change: 729, hour: 9:14, term_id: 2522115, start_epoch: 1447168440…}



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?






UPDATE



Ok so sometimes the JSON is invalid apparently and sometimes not, so far I'm doing good without errors with the following snippet, thoughts?



if (typeof alert === 'object') {
// do nothing...
} else {
var alert = JSON.parse(alert);
}


Most of the time the alert result of JSON.parse(message.data) is a string so I need the other check to double parse it.


More From » json

 Answers
16

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.


[#61258] Friday, July 22, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
corie

Total Points: 371
Total Questions: 110
Total Answers: 113

Location: Cambodia
Member since Thu, Oct 7, 2021
3 Years ago
;