1

I'm trying to extract just the response from the following response

"{"Message":"Looks like you need to login"}"

I tried to stringyfy it as follows

var response = "{"Message":"Looks like you need to login"}";
var json_response = JSON.stringify(response);

But my response ends up looking something like this.

"{\"Message\":\"Looks like you need to login\"}"

Any ideas on why this is happening? and how I can extract just the message by doing something like

json_response.Message perhaps?

j08691
  • 204,283
  • 31
  • 260
  • 272
BaconJuice
  • 3,739
  • 13
  • 56
  • 88

3 Answers3

3

You need to use JSON.parse():

var str = "{\"Message\":\"Looks like you need to login\"}";
var json = JSON.parse(str);
console.log(json.Message);
Spencer Wieczorek
  • 21,229
  • 7
  • 44
  • 54
0

Try this:

var response = { Message: "Looks like you need to login" } ;
var json_response = JSON.stringify( response ) ;
Heimetli
  • 23
  • 5
  • 2
    While this code block may answer the OP's question, this answer would be much more useful if you explain how this code is different from the code in the question, what you've changed, why you've changed it and why that solves the problem without introducing others. – Mifeet Sep 08 '15 at 16:32
0

You need to use parse() method of json which is very useful. So keep using that it is very light weighted.

Here is my answer :

var myString = "{\"Message\":\"Looks like you need to login\"}";
var parsedJson = JSON.parse(myString);
alert(parsedJson.Message);
Nisarg Raval
  • 160
  • 12