0%

SyntaxError: "[object Object]" is not valid

When you try to directly parse a server side responed json to a JSON object ,it will popup an err.

1
2
3
4
5
const message = {"code":"200","msg":"Sample movement recorded succsfully"} 

var msg = JSON.parse(message);
// Log to console
console.log(msg.msg)
1
2
3
SyntaxError: "[object Object]" is not valid JSON
at <anonymous>:6:16
at mn (<anonymous>:16:5455)

you need to use JSON.stringify() method first, then parse the data to JSON object

1
2
3
4
5
6
const message = {"code":"200","msg":"Sample movement recorded succsfully"} 

var obj = JSON.stringify(message);
var msg = JSON.parse(obj);
// Log to console
console.log(msg.msg)