I'm trying to write JavaScript to use parsed JSON, however the JSONP has a variable root, like this:
{"AAPL":{"quote":{"symbol":"AAPL","change":-3.34}},
"FB":{"quote":{"symbol":"FB","change":1.03}},
"TSLA":{"quote":{"symbol":"TSLA","change":2.9}}}
var stocks = ['AAPL', 'FB', 'TSLA'];
var stocksUrl = stocks.join(',');
var url = 'https://api.iextrading.com/1.0/stock/market/batch?symbols=' + stocksUrl + '&types=quote';
$.getJSON(url, function(data) {
After parsing with jquery, I can reference the JavaScript object such as:
console.log(JSON.stringify(data.AAPL.quote.symbol));
}
However, since the length of the object is variable and the root element is variable, I am not sure how to use a for loop to iterate through the symbol and change elements.
My question is how to remove a variable root element and keep the sub-elements so that the object afterword would be referenced such that quote became the root element, ie:
for (var i = 0; i < Object.keys(data).length; i++) {
console.log(JSON.stringify(data.quote.symbol));
}
I have tried Remove outer Object from certain key but keep inner objects but that solution is for a known element name. Can I do something similar with a variable object name? Or is there a better way to solve the problem?
Thanks in advance.