Embarking on my first JSON project has brought some challenges that I am struggling to overcome.
Here's the task at hand. I have created a basic scraper using Apify.com to extract data from a specific website. The data is presented in JSON format, and here is a snippet of what I am receiving:
[{
"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
}
}]
Transitioning back to HTML and JavaScript, I have tried to extract the 'nowPlaying' variable using the following code. However, I encounter an issue where the console log displays the same data as before, but the 'nowPlaying' variable returns as 'undefined.'
There seems to be a glaring oversight that I am unable to pinpoint, preventing me from accessing the desired data. Any ideas on how I can retrieve the text "Four Corners Music Show September 16th 2019 - hosted by Melinki," split it, and insert it into the correct 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];
}