0

I have this javascript object,

console.log(object.response);
console.log(object.response.imageUID);

gives me:

{"id":138,"imageUID":"image-aa0dce87-0261-44ef-8377-d897a996f4b1"}  
undefined  

What went wrong?

10
  • Is response a string or another object? Commented Oct 9, 2014 at 17:41
  • response is an object Commented Oct 9, 2014 at 17:41
  • 1
    Try console.log(object.response["imageUID"]); Commented Oct 9, 2014 at 17:42
  • @blackops: no won't do, still undefined. Commented Oct 9, 2014 at 17:44
  • 3
    console.log(JSON.parse(object.response).imageUID); Commented Oct 9, 2014 at 17:45

1 Answer 1

3

It seemed the issue was that your response was a stringified object - so it probably looked like:

var response = '{"id":138,"imageUID":"image-aa0dce87-0261-44ef-8377-d897a996f4b1"}';

In order to access the properties you must first parse that string into an object:

var responseObj = JSON.parse(response);

Now you can access the properties:

var imageUID = responseObj.imageUID;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.