0

I am creating a google chrome extension as a project and have run into a hurdle. I am playing around with JSON (hoping to eventually use an API i like). I have an example JSON file, load it, get a variable storing the parsed JSON (which I can see as an object, containing objects and values), however, I don't know how to reference values of the objects within.

var xhr = new XMLHttpRequest();
var resp;
xhr.open("GET", "eg.json", true);
xhr.onreadystatechange = function() {
  if (xhr.readyState == 4) {
    resp = JSON.parse(xhr.responseText);
  }
}
xhr.send();

The object resp looks like this after send is executed.

I guess this is because I'm new to JavaScript but how would I get the createdDate as a String variable?

Thanks

1 Answer 1

1

You can access it via dot or bracket (more for dynamic properties) notation (or a mix of both) like any other nested objects. For example, based on the image you posted it would be:

var createdDate = resp.WhoisRecord.audit.createdDate;

Note you need to call this inside that if where resp is defined, only in that callback will it be populated...if you try and use it after your xhr.onreadystatechange = function hookup, it won't be ready when it runs, inside that callback with a readyState == 4, it should be.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the response, I had that line correct gladly and it appears you have found my real problem, getting a variable out of that function. How would I go about this since I can't see a way to access its return?

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.