-4
{ "errors":
          [
            { "value":"k@d.c",
              "msg":"Email is not valid",
              "param":"email",
              "location":"body"
            }
          ]
 }

I wanna extract the msg from this json object. How do i do this?

Code Maniac
  • 37,143
  • 5
  • 39
  • 60

2 Answers2

1
const jsonContent = `{"errors":[{"value":"k@d.c","msg":"Email is not valid","param":"email","location":"body"}]}`;
const jsObject = JSON.parse(jsonContent);
const errorMessage = jsObject.errors[0].msg;

This will result in:

{ 
   "errors":[ 
      { 
         "value":"k@d.c",
         "msg":"Email is not valid",
         "param":"email",
         "location":"body"
      }
   ]
}

[UPDATE] Sorry, didn't see you asked just for the msg property. Edited my code.

0

You can use JSON.parse(str) like so

var t = '{"errors":[{"value":"k@d.c","msg":"Email is not valid","param":"email","location":"body"}]}';
var msg = JSON.parse(t)["errors"][0]["msg"];
ege
  • 774
  • 5
  • 19