0

I can't seem to figure out how to get this code snippet to work. I am trying to access the 'Name' object in this json snippet. Any help would be appreciated.

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js">
</script>
<script>
$(document).ready(function () {
	$.getJSON('https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22AAPL%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=', function(data) {
    	$('#demo').text(data[0].Name);
    });
});
</script>
</head>
<body>

<p id="demo"></p>

</body>
</html>

1
  • Please provide a sample of the actual JSON in the question itself. There may be people who can help you, but can't access the API url. Commented Feb 13, 2017 at 18:39

3 Answers 3

1

Use this:

$(document).ready(function () {
    $.getJSON('https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22AAPL%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=', function(data) {
        $('#demo').text(data.query.results.quote.Name);
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! I see that i need to access each object before I grab the one I want
You're welcome. You might take a look at this for reference: w3schools.com/js/js_json.asp
0
{
    "query": {
        "count": 1,
        "created": "2017-02-13T18:34:48Z",
        "lang": "es-419",
        "results": {
            "quote": {
              "name": "Blabla"

So you have data.query.results.quote.name

Comments

0

Your API call does not return an array, it returns a JSON object.

Try: $('#demo').text(data.query.results.quote.Name);

Here's what the data structure that is being returned looks like:

{
    "query": {
        "count": 1,
        "created": "2017-02-13T18:34:12Z",
        "lang": "en-us",
        "results": {
            "quote": {
                // other props...
                "Name": "Apple Inc.",
                // other props...
            }
        }
    }
}

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.