vishnu
vishnu

Reputation: 4599

Remove back slashes from JSON response

I am getting the JSON error response from server in the following way,

let err = {
        "_body": "{\"error\":\"264\",\"message\":\"Please enter valid usename/password\",\"object\":null}",
        "status": 400,
        "ok": false
    }

And i want to display the error message on the screen 'Please enter valid usename/password'

I tried in the following way but no luck,

console.log((this.err._body).replace(/\\/g, ''));

Upvotes: 4

Views: 7623

Answers (2)

Maximilian Riegler
Maximilian Riegler

Reputation: 23506

You probably need to decode the wrapped json string again:

let responseBody = JSON.parse(this.err._body);
console.log(responseBody.message);

Upvotes: 4

Martin
Martin

Reputation: 16292

You just need to deserialize the body.

let err = {
        "_body": "{\"error\":\"264\",\"message\":\"Please enter valid usename/password\",\"object\":null}",
        "status": 400,
        "ok": false
    }

var body = JSON.parse(err._body);
console.log(body.message);

Click on Run code snippet to see this working.

Upvotes: 10

Related Questions