I am doing my first project using JSON but so far I am getting stuck on something and don't see a solution.
What do I want to do. I built a simple scraper using Apify.com that returns some information from a site I need. They say the format is JSON, so when I load in the file this is what I get:
[{
"nowPlaying": "Four Corners Music Show September 16th 2019 - hosted by Melinki",
"#error": false,
"#debug": {
"requestId": "aHg5UyCT6vWQhSD",
"url": "http://www.example.com/example/",
"loadedUrl": "http://www.example.com/example/",
"method": "GET",
"retryCount": 0,
"errorMessages": null,
"statusCode": 200
}
}]
Now switching back to HTML and javascript I use this code to load the data and try and extract the first variable nowPlaying. But the problem that I have is that I see in the first console.log the exact same data as I showed before, so it seemed to have loaded the file. But when asking for the nowPlaying variable I see a 'undefined' appearing in the console.
There must be something obvious I totally miss, but I can't seem to get to the data I need. Any suggestions on how to get the text " Four Corners Music Show September 16th 2019 - hosted by Melinki" in a variable that I can split and put in the right html elements?
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
processData(this);
}
};
xhttp.open("GET", "https://api.apify.com/v2/actor-tasks/YSMp66SiktwNXocsf/runs/last/dataset/items?token=twn8q5PnsM5s485DNtxzabdcP&ui=1", true);
xhttp.send();
function processData(data) {
var apiData = data.response;
console.log(apiData);
console.log(apiData.nowPlaying);
var programmingInfo = apiData.nowPlaying.split('-');
document.getElementById("showName").innerHTML = programmingInfo[0];
document.getElementById("host").innerHTML = programmingInfo[1];
}