Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove back slashes from JSON response

Tags:

javascript

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, ''));
like image 982
vishnu Avatar asked Oct 21 '25 13:10

vishnu


2 Answers

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.

like image 178
Martin Avatar answered Oct 23 '25 02:10

Martin


You probably need to decode the wrapped json string again:

let responseBody = JSON.parse(this.err._body);
console.log(responseBody.message);
like image 26
rinukkusu Avatar answered Oct 23 '25 03:10

rinukkusu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!