I am receiving a JSON object from a http call and I am trying to extract values from it. JSON object contains:
data:{\"userid\":\"007\", \"role\":\"spy\"}
According to the second line of your log (the call to JSON.stringify()
), your data
is actually an array of objects:
[{"userid":"007", "role":"spy"}]
If it was an object as you are expecting, it would look like this:
{"userid":"007", "role":"spy"}
(the difference is subtle, but notice the missing square brackets)
Try this:
currentUserRole = data[0].role;
Obviously in production-ready code, you probably need to do some extra sanity checking to ensure that data
is in fact an array containing at least one element.
It is a list. Try data[0].role