Greg Gum
Greg Gum

Reputation: 37909

How to get reference to unnamed array from json

I am getting this back from the server:

{"Message":"The request is invalid.","ModelState":{"":["Name [email protected] is already taken.","Email '[email protected]' is already taken."]}}

How do I reference the first item of the array in JS? The debugger tells me that ModelState is an object, but there seems to be no name for the array.

Upvotes: 1

Views: 342

Answers (2)

turbopipp
turbopipp

Reputation: 1255

You can reference the first item of your unnamed array like this:

var jsonString = {"Message":"The request is invalid.","ModelState":{"":["Name [email protected] is already taken.","Email '[email protected]' is already taken."]}};

console.log('first item: ', jsonString.ModelState[''][0]);

Upvotes: 2

Web and Flow
Web and Flow

Reputation: 143

if test = {"Message":"The request is invalid.","ModelState":{"":["Name [email protected] is already taken.","Email '[email protected]' is already taken."]}}

then your array is at test['ModelState'][""]

Upvotes: 1

Related Questions