user285594
user285594

Reputation:

JavaScript - why json stringify not working?

Why jsonObject.msg is undefined? even i am getting value as {"msg":"not_inserted"} from response.farewell.

( FYI: where response.farewell is Google chrome apps generated JSON value sendMessage("{\"msg\" : \"" + tmp + "\"}"); )

//var reply ='{"msg":"not_inserted"}';
//var jsonstring = JSON.parse(reply);
//console.log('>>> ' , jsonstring.msg); // it WORKS!


try {
    console.log("REPLY: ", response.farewell); 

    // it FAILS!!
    var jsonString = JSON.stringify(response.farewell);        
    var jsonObject = JSON.parse(jsonString);        
    console.log('>>> ' , jsonObject.msg);

  } catch(eio) {        
    console.log(eio);
  }

Output:

REPLY: {"msg":"not_inserted"}
>>> undefined 

Expected output:

>>> not_inserted

Upvotes: 0

Views: 1736

Answers (1)

guest271314
guest271314

Reputation: 1

Try using single quotes to wrap string passed to sendMessage, removing extra double quotes

sendMessage('{\"msg\" : \"' + tmp + '\"}')

Upvotes: 1

Related Questions