0

What is the correct syntax needed here for my variable text?

$.getJSON('https://www.alphavantage.co/query? 
function=BATCH_STOCK_QUOTES&symbols=MSFT&apikey=TRZC92S2OMSWBC8H', 
function(data) {

    var text = `Date: ${data.{"Stock Quotes"}.[{"1. symbol"}]}<br>
                Time: ${data.{"Stock Quotes"}.[{"2. price"}]}<br>`


    $(".mypanel").html(text);
});

The JSON looks like this in raw form:

{
    "Meta Data": {
        "1. Information": "Batch Stock Market Quotes",
        "2. Notes": "IEX Real-Time Price provided for free by IEX (https://iextrading.com/developer/).",
        "3. Time Zone": "US/Eastern"
    },
    "Stock Quotes": [
        {
            "1. symbol": "MSFT",
            "2. price": "100.0600",
            "3. volume": "18924367",
            "4. timestamp": "2018-07-02 16:45:04"
        }
    ]
}

This question is different from the other question in that it uses the getJSON function.

  • *"This question is different from the other question in that it uses the getJSON function."* Why should that make it different? `data` is an object and you are trying to access a specific (nested) property. The other question tells you everything you need to know to do that: It tells how to access properties whose names have spaces, how to access arrays and how to chain these to access nested properties. – Felix Kling Jul 03 '18 at 00:11
  • Why do you feel put down? I tried to explain how these questions are the same and that it contains the information you need. *"Not everybody is well versed on objects."* Nor is there an expectation that they should. Which is exactly why the other question exists. It tries to concentrate the most important information for accessing objects. – Felix Kling Jul 03 '18 at 14:53
  • *"For some of us, this kind of thing is at the very fringes of a 1000 other things we do,"* Maybe that is the fundamental problem and difficult to rectify, I don't know. However, it goes the other way as well. We also 1000 things to do and don't want to spend time explaining the same thing over and over again, which is why some of us spend considerable time *once* to create canonical answers. *"Grow up computer boy."* I'm not the person who is attacking someone else, so I think I'm doing fine on that front, thank you :) – Felix Kling Jul 03 '18 at 14:55
  • This is how you'd access data in the inner array: `data["Stock Quotes"][0]["1. symbol"]`. Any property can be accessed using bracket notation so we just do that "all the way down". – Felix Kling Jul 03 '18 at 15:06
  • And to be clear, it's also perfectly fine to not understand the solution in an existing question. But then you can just say so. Claiming that your question is different only gives the impression that you haven't even bothered looking at the other question. And attacking people isn't helping anyone... – Felix Kling Jul 03 '18 at 15:44

0 Answers0