4

i have JSON response which is having backslashes and some responses are not containing the backslashes.

I need to show error message based on the response, How do i parse the JSON response using javascript?

JSON response with out backslashes,

{"_body":{"isTrusted":true},"status":0,"ok":false,"statusText":"","headers":{},"type":3,"url":null} 

response with backslashes,

{"_body":"{\"timestamp\":\"2016-11-18T04:46:18.972+0000\",\"status\":500,\"error\":\"Internal Server Error\",\"exception\":\"java.lang.ArrayIndexOutOfBoundsException\",\"message\":\"1\",\"path\":\"/login\"}","status":500,"ok":false,"statusText":"Internal Server Error"}

i tried in the following way but it is working only for JSON response which is not having the backslashes.

var strj = JSON.stringify(err._body);
 var errorobjs = strj.replace(/\\/g, "");
6
  • if it has backslashes its not a valid json but a string Commented Nov 18, 2016 at 5:02
  • 1
    @madalinivascu backslashes are valid in json.The problem is not backslashes but, the json format is invalid. He is wrapping obj in string. Commented Nov 18, 2016 at 5:15
  • 1
    Both of the above are valid json. The second one just has a field that contains a json string itself, and so needs to be JSON parsed again. See my answer. Commented Nov 18, 2016 at 5:17
  • 1
    @Joe but, that's never a good option .. to parse json and read obj as string and then parse it again. Commented Nov 18, 2016 at 5:18
  • 1
    I agree, but we have no idea if this person controls the server code at all. We can't just assume he can change the server code, we need to provide an answer given the constraints. Commented Nov 18, 2016 at 5:20

3 Answers 3

3

Actually the problem is not with / slashs. The JSON is INVALID.

remove these " from backend server

{"_body":"{\"timestamp\":\"2016-11-18T04:46:18.972+0000\",\"status\":500,\"error\":\"Internal Server Error\",\"exception\":\"java.lang.ArrayIndexOutOfBoundsException\",\"message\":\"1\",\"path\":\"/login\"}","status":500,"ok":false,"statusText":"Internal Server Error"}

double quote before "{"timestamp and one after login"}" these two highlighted and your code will work.

var data = '{"_body":{\"timestamp\":\"2016-11-18T04:46:18.972+0000\",\"status\":500,\"error\":\"Internal Server Error\",\"exception\":\"java.lang.ArrayIndexOutOfBoundsException\",\"message\":\"1\",\"path\":\"/login\"},"status":500,"ok":false,"statusText":"Internal Server Error"}';

var json_data = JSON.parse(data);

console.log(json_data);

You are actually wrapping body object in string at backend which is not valid.

"body" : "bodyOBJ"  //Invalid
"body" : bodyObj    //Valid
Sign up to request clarification or add additional context in comments.

Comments

1
var obj = JSON.parse(response)

if(typeof obj._body == "string") {
    obj._body = JSON.parse(obj._body)
}

console.log(obj);

Comments

1

Solution:

var response = {"_body":"{\"timestamp\":\"2016-11-18T04:46:18.972+0000\",\"status\":500,\"error\":\"Internal Server Error\",\"exception\":\"java.lang.ArrayIndexOutOfBoundsException\",\"message\":\"1\",\"path\":\"/login\"}","status":500,"ok":false,"statusText":"Internal Server Error"};
var body = JSON.parse(response._body);
console.log(body.error);

Explanation:

You have a top level object with one key, _body. The value of that key is a string containing JSON itself. This is usually because the server side code didn't properly create the JSON. That's why you see the \" inside the string. Unless you are able to fix the server side code, you have to decode the nested JSON manually.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.