0

How do I get the message and status values from this response

{"errors":[{"code":3,"message":"Invalid login details"}],"status":"failure"}

I've tried:

data.message
data['errors'].message
data[0].message

data.status
data[0].status

Edit:

I am parsing a res like this

 var data = jQuery.parseJSON(jQuery(res).find('#container').text());

 console.log(data.status);
 console.log(data.errors[0].message);

The string I'm parsing is:

"{\"errors\":[{\"code\":3,\"message\":\"Invalid login details\"}],\"status\":\"failure\"}"

Answer:

The response I was getting was already being encoded as JSON, so once I made it a plain echo and kept the parsing on the client side via js and stripped the slashes from the res it now works as expected. Thank you all for commenting as it helped me a lot figure out what I was doing wrong

Pierre
  • 46
  • 4

2 Answers2

0

Errors is an array so you need to access it by index.

const data = {"errors":[{"code":3,"message":"Invalid login details"}],"status":"failure"}

console.log(
  data.errors[0].code,
  data.errors[0].message,
  data.status
)
synthet1c
  • 6,152
  • 2
  • 24
  • 39
0

first of all, are you sure that you have an object and not just a JSON string? you may need to call JSON.parse(data);

then it looks like you're looking for data.errors[0].code or data.errors[0].message or data.status

stackoverfloweth
  • 6,669
  • 5
  • 38
  • 69
  • var data = jQuery.parseJSON(jQuery(res).find('#container').text()); console.log(data.status); console.log(data.errors[0].message); – Pierre Mar 17 '17 at 00:42
  • have you confirmed that `jQuery(res).find('#container').text()` equals what you think? – stackoverfloweth Mar 17 '17 at 00:43
  • yea I get the response I want which is parsed correctly as shown above with the JSON obj – Pierre Mar 17 '17 at 00:45
  • "{\"errors\":[{\"code\":3,\"message\":\"Invalid login details\"}],\"status\":\"failure\"}" – Pierre Mar 17 '17 at 00:46
  • try using `JSON.parse` instead of `jQuery.parseJSON`. I've never heard of the jQuery parse – stackoverfloweth Mar 17 '17 at 00:46
  • I'm assuming that's the log before you parse it – stackoverfloweth Mar 17 '17 at 00:46
  • I have when I saw your comment but same outcome – Pierre Mar 17 '17 at 00:48
  • yea the string with slashes is before parsing – Pierre Mar 17 '17 at 00:49
  • it's working here https://jsfiddle.net/h2fpsxus/ – stackoverfloweth Mar 17 '17 at 00:51
  • I've moved the res string to it's own var to follow the same format as in your fiddle and then added that to JSON.parse(var) but still getting the same outcome, don't know why as it should be straight forward. Must be missing something – Pierre Mar 17 '17 at 00:56
  • After the JSON.parse(var) I'm logging the typeof and it's coming back as a string – Pierre Mar 17 '17 at 00:57
  • hit update and send me the fiddle link – stackoverfloweth Mar 17 '17 at 00:57
  • I can't update your fiddle as the string I'm getting is an Ajax res from a WP site; the response is sending back the full page DOM therefore I'm doing the .find() on the res to get the JSON string I need which I want to use to show various messages based on the status etc. As I say after the JSON.parse() it's still saying it's a string, I've also tried JSON.stringify() on the string before the JSON.parse() as well – Pierre Mar 17 '17 at 01:05