0

I want to create RegEx validator for my JSON response data, but I could not create successfully. What I am doing wrong? Please help me in this regards, thanks.

If service returns an error, response json must be as follows;

{
 "status":"error",
 "error":{
   "connection timeout"
 }
}

If service returns a success, response json must be as follows;

{
   "status":"success",
   "result":{
      "username":"msmumbucoglu",
      "password":"123456",
      "name":"Hacked By Mehmet:)"
   }
}

My regex is as follows:

(?:"status":")|(?:"result":")|(?:"error":")(.*?)(?:")
Antoine Boisier-Michaud
  • 1,575
  • 2
  • 16
  • 30
Baltazarr
  • 35
  • 1
  • 10

1 Answers1

0

You can do it by parsing the string received (your json string) to a json object and then read the property status of this object.

const myJsonString = '{"status":"error","error":{"connection timeout"}}';    
const response = JSON.parse(myJsonString);

if (response.status === 'success') {
    console.log('OK');
} else {
    console.log('ERROR');
}

// in this case output will be: ERROR
Dave
  • 1,912
  • 4
  • 16
  • 34