1

I know it's probably simple, but I'm receiving this object from NASA API and I'm not being able to access the object parameter named "2020-01-02".

I'm receiving the "error parsing" message if I try to access like Object.near_earth_objects."2020-01-02" or Object.near_earth_objects.2020-01-02, or any other variation of it, which makes sense for sure.

{
"links": {
"next": "http://www.neowsapp.com/rest/v1/feed?start_date=2020-01-03&end_date=2020-01-03&detailed=false&api_key=A8QOwQlPLCc2EYkcNhYxj1SZKIHh83v6tkp55y7e",
"prev": "http://www.neowsapp.com/rest/v1/feed?start_date=2020-01-01&end_date=2020-01-01&detailed=false&api_key=A8QOwQlPLCc2EYkcNhYxj1SZKIHh83v6tkp55y7e",
"self": "http://www.neowsapp.com/rest/v1/feed?start_date=2020-01-02&end_date=2020-01-02&detailed=false&api_key=A8QOwQlPLCc2EYkcNhYxj1SZKIHh83v6tkp55y7e"
},
"element_count": 13,
"near_earth_objects": {
  "2020-01-02": [other parameters...] 
}
Eric
  • 11
  • 3
  • Need to use a [bracket notation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors). – Phix Jan 02 '20 at 16:33
  • Access it like this `Object.near_earth_objects["2020-01-02"]` – Ramesh Reddy Jan 02 '20 at 16:35
  • 2
    Does this answer your question? [How can I access and process nested objects, arrays or JSON?](https://stackoverflow.com/questions/11922383/how-can-i-access-and-process-nested-objects-arrays-or-json) – Taplar Jan 02 '20 at 16:35

3 Answers3

1

You can not access like that... I see two reasons for that, one you are starting the property name with a number and you use '-' in the middles as well.

That way you must you [] to access the property, like this:

near_earth_objects['2020-01-02']

1

This snippet gives you access to value inside "2020-01-02" which currently "test"

let data = {
    "links": {
        "next": "http://www.neowsapp.com/rest/v1/feed?start_date=2020-01-03&end_date=2020-01-03&detailed=false&api_key=A8QOwQlPLCc2EYkcNhYxj1SZKIHh83v6tkp55y7e",
        "prev": "http://www.neowsapp.com/rest/v1/feed?start_date=2020-01-01&end_date=2020-01-01&detailed=false&api_key=A8QOwQlPLCc2EYkcNhYxj1SZKIHh83v6tkp55y7e",
        "self": "http://www.neowsapp.com/rest/v1/feed?start_date=2020-01-02&end_date=2020-01-02&detailed=false&api_key=A8QOwQlPLCc2EYkcNhYxj1SZKIHh83v6tkp55y7e"
    },
    "element_count": 13,
    "near_earth_objects": {
        "2020-01-02": "test"
    }
};

console.log(data.near_earth_objects["2020-01-02"]);
AlexZeDim
  • 3,520
  • 2
  • 28
  • 64
0

Try this one

var k = {
  "element_count": 13,
  "near_earth_objects": {
    "2020-01-02": [{
      "one": "one",
      "two" : "two"
    }]
  }
}
var a = k.near_earth_objects["2020-01-02"]
console.log(a)

And you will get the content of "2020-01-02" like this :

[ { one: 'one', two: 'two' } ]