Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

129
Views
Unable to get object with date format from JSON file in JavaScript

I'm doing an activity with JavaScript where I must call an extern API and get the information that it contains, the JSON file structure is the next:

"dates": {
    "2020-03-22": {
        "countries": {

The problems is that when I get the information I put it in a variable named 'info' and to arrive to the date camp I have put the date text in another variable to concatenate with the previous object, but the problem is that returns 'undefined' value.

Here's the code I made to get the information:

function callAPI() {
    var xmlhttp = new XMLHttpRequest();

    xmlhttp.onreadystatechange = function() {
        if(this.readyState == 4 && this.status == 200){
            var info = JSON.parse(this.responseText);
            var dateApi = "2020-03-22";

            console.log(info.dates.dateApi);
        }
    }
    xmlhttp.open("GET", "https://api.covid19tracking.narrativa.com/api/2020-03-22/country/spain");
    xmlhttp.send();
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Change info.dates.dateApi code to info.dates[dateApi] this should work

function callAPI() {
    var xmlhttp = new XMLHttpRequest();

    xmlhttp.onreadystatechange = function() {
        if(this.readyState == 4 && this.status == 200){
            var info = JSON.parse(this.responseText);
            var dateApi = "2020-03-22";

            console.log(info.dates[dateApi]);
        }
    }
    xmlhttp.open("GET", "https://api.covid19tracking.narrativa.com/api/2020-03-22/country/spain");
    xmlhttp.send();
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!