Error message is showing like below.
"{\"errorCode\":\"2029\",\"errorMessage\":\"Duplicate Entry\"}" How do I extract errorCode from above in javascript?
This is a JSON encoded object. In order to access the properties of the encoded object, you need to json decode it first. You can use JSON.parse(). Then you can get its properties simply by property name.
Eg:
let encodedString = "{\"errorCode\":\"2029\",\"errorMessage\":\"Duplicate Entry\"}"; let decodedObj = JSON.parse(encodedString); console.log(decodedObj.errorCode); // prints "2029" You can use parseInt() to extract the error code as a number rather than a string. Eg:
console.log(parseInt(decodedData.errorCode)); // prints 2029 1 is valid JSON, so is "Hello".