0

I'm using the NASA Near Earth Object array for a student project and am having trouble accessing nested objects that have a date and hyphens for a key e.g.

2016-09-08 : [...]

The error I get back is just 'undefined'.

The API call I have is:

$(document).ready(function NASAtest() {
    $.ajax({
        type: "GET",
        url: "https://api.nasa.gov/neo/rest/v1/feed?start_date=2016-09-07&end_date=2016-09-08&api_key=DEMO_KEY",
        asynch: false,
        contentType: "application/javascript",
        dataType: "json",    
        success: function(data) {
           console.log(data)
           var recordList = data.near_earth_objects;
           console.log(recordList);
           var recordList2 = data.near_earth_objects[2016-09-08];
           console.log(recordList2);

            }     
    });  
});

Sample API data:

{
    "near_earth_objects": {
        "2016-09-08": [
            {
                "neo_reference_id": "3726710",
                "name": "(2015 RC)",
                "nasa_jpl_url": "http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=3726710",
                "absolute_magnitude_h": 24.3,
                "is_potentially_hazardous_asteroid": false,
            } ] } }

See fiddle: https://jsfiddle.net/lollyborch/v640ocfr/

and JSON data: https://api.nasa.gov/neo/rest/v1/feed?start_date=2016-09-07&end_date=2016-09-08&api_key=DEMO_KEY

I'd eventually like to iterate over all the date info to get down to the keys like "absolute_magnitude_h" and "is_potentially_hazardous_asteroid" for a date range but at this stage am just having trouble getting past the date key.

I've tried using square brackets rather than dot notation as described here and here but can't seem to see what I'm doing wrong.

Any ideas on the right direction would be much appreciated.

Community
  • 1
  • 1
LollyBee
  • 35
  • 1
  • 5
  • 3
    Did you try with quotes: `data.near_earth_objects["2015-09-08"]`? – Paul Apr 11 '16 at 13:46
  • Additionally, please note that you are retrieving data for 201**6**, but you try to get data for a date in 201**5**, so I guess you want `data.near_earth_objects["2016-09-08"]`. Just in case you are still wrapping your head around the "undefined" error. See https://jsfiddle.net/v640ocfr/1/ – Paul Apr 11 '16 at 13:51
  • Thanks so much @Paul. Kicking myself that it was a typo error. I've tried so many variations tonight I hadn't remembered to update. – LollyBee Apr 11 '16 at 13:57

2 Answers2

3

The property name is 2015-09-08, however, you're accessing key 1998 (2015 - 9 - 8). You need to wrap it in quotes, otherwise it will evaluate the expression and use the result of it as the property name:

data.near_earth_objects["2015-09-08"]

As a side note, since you mention dot notation in your question, this is one case where you have to use bracket notation rather than dot notation, as the equivalent property name wouldn't work in dot notation.


Having said that, as you move further down this route, you'll probably end up iterating over the contents of the object rather than using hardcoded names, at which point the issue sort of resolves itself:

for (var neo in data.near_earth_objects) {
    //neo is now, for instance, "2016-09-07"
    console.log(neo, data.near_earth_objects[neo])
}
James Thorpe
  • 31,411
  • 5
  • 72
  • 93
  • Thanks. Combination of adding quotes and fixing a typo (2016 vs 2015) worked. You're right - I should have gone to the next step of a for loop which would have solved both of these problems. – LollyBee Apr 11 '16 at 14:01
0

When using bracketed notation, you may want to consider using quotes around the key you are attempting to access :

var recordList2 = data.near_earth_objects['2016-09-08'];

You can see an example of this being accessed in the console below :

enter image description here

Rion Williams
  • 74,820
  • 37
  • 200
  • 327