Looping through a JSON object containing more objects and arrays

iterate through json object java
loop through nested json object javascript
iterate through json object python
iterate list of json objects java
iterate json object in typescript
iterate over nested objects javascript
how to iterate multiple json object in java
how to get key and value from json array object in javascript

I have a JSON object of asteroid data of the following form:

    {
  "links" : {
    "next" : "https://api.nasa.gov/neo/rest/v1/feed?start_date=2018-06-14&end_date=2018-06-21&detailed=false&api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS",
    "prev" : "https://api.nasa.gov/neo/rest/v1/feed?start_date=2018-05-31&end_date=2018-06-07&detailed=false&api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS",
    "self" : "https://api.nasa.gov/neo/rest/v1/feed?start_date=2018-06-07&end_date=2018-06-14&detailed=false&api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS"
  },
  "element_count" : 40,
  "near_earth_objects" : {
    "2018-06-09" : [ {
      "links" : {
        "self" : "https://api.nasa.gov/neo/rest/v1/neo/2162474?api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS"
      },
      "neo_reference_id" : "2162474",
      "name" : "162474 (2000 LB16)",
      "nasa_jpl_url" : "http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=2162474",
      "absolute_magnitude_h" : 18.5,
      "estimated_diameter" : {
        "kilometers" : {
          "estimated_diameter_min" : 0.5303407233,
          "estimated_diameter_max" : 1.1858779086
        },
        "meters" : {
          "estimated_diameter_min" : 530.3407233187,
          "estimated_diameter_max" : 1185.8779085771
        },
        "miles" : {
          "estimated_diameter_min" : 0.3295383456,
          "estimated_diameter_max" : 0.7368701419
        },
        "feet" : {
          "estimated_diameter_min" : 1739.963058693,
          "estimated_diameter_max" : 3890.675677576
        }
      },
      "is_potentially_hazardous_asteroid" : true,
      "close_approach_data" : [ {
        "close_approach_date" : "2018-06-09",
        "epoch_date_close_approach" : 1528527600000,
        "relative_velocity" : {
          "kilometers_per_second" : "29.5006815383",
          "kilometers_per_hour" : "106202.4535379664",
          "miles_per_hour" : "65990.074533077"
        },
        "miss_distance" : {
          "astronomical" : "0.2778177136",
          "lunar" : "108.0710983276",
          "kilometers" : "41560940",
          "miles" : "25824772"
        },
        "orbiting_body" : "Earth"
      } ]
    "2018-06-07" : [...
        ]

I am trying to loop through it, accessing just the name of each asteroid within the array of objects associated with todays date "2018-06-07", displaying the name of each in an html page

Here's what I have tried so far:

// get JSON data for todays asteroids
$.getJSON("https://api.nasa.gov/neo/rest/v1/feed?start_date="+today+"&api_key="+API_KEY, function(result) {


    // iterate over all keys
    for (let x in result) {

        // if key is 'near_earth_objects'
        if (x == 'near_earth_objects') {

            // iterate over dates in 'near_earth_objects'
            for (y in result.near_earth_objects) {

                // if todays date
                if (y == result.near_earth_objects[today]) {

                    // loop over array of objects for todays date
                    for (let i=0; i<result.near_earth_objects[today].length; i++) {


                        let id = 'n'+(i+1);

                        let output = document.getElementById(id);
                        output.innerhtml = result.near_earth_objects[today][i].name;
                    }
                }
            }
        }
    }

However my html divs are showing blank. Any ideas?

p.s. 'today' is a variable i've already defined to be todays date with the correct format "yyyy-mm-dd"

I'm not sure why you are using loops to select the property. You directly select the right array. Then loop over its elements and append them to the document.

I took some more data from nasa.gov to show you:

result.near_earth_objects["2018-06-18"].forEach(object => {

  const el = document.createElement('div')
  el.innerHTML = object.name

  document.querySelector('body').appendChild(el)

});
<script>
  const result = {  
   "links":{  
      "next":"https://api.nasa.gov/neo/rest/v1/feed?start_date=2018-06-21&end_date=2018-06-28&detailed=false&api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS",
      "prev":"https://api.nasa.gov/neo/rest/v1/feed?start_date=2018-06-07&end_date=2018-06-14&detailed=false&api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS",
      "self":"https://api.nasa.gov/neo/rest/v1/feed?start_date=2018-06-14&end_date=2018-06-21&detailed=false&api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS"
   },
   "element_count":38,
   "near_earth_objects":{
      "2018-06-18":[  
         {  
            "links":{  
               "self":"https://api.nasa.gov/neo/rest/v1/neo/3825034?api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS"
            },
            "neo_reference_id":"3825034",
            "name":"(2018 KR2)",
            "nasa_jpl_url":"http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=3825034",
            "absolute_magnitude_h":19.136,
            "estimated_diameter":{  
               "kilometers":{  
                  "estimated_diameter_min":0.3956899106,
                  "estimated_diameter_max":0.884789538
               },
               "meters":{  
                  "estimated_diameter_min":395.6899105531,
                  "estimated_diameter_max":884.7895380075
               },
               "miles":{  
                  "estimated_diameter_min":0.2458702354,
                  "estimated_diameter_max":0.54978256
               },
               "feet":{  
                  "estimated_diameter_min":1298.1952861389,
                  "estimated_diameter_max":2902.8529078765
               }
            },
            "is_potentially_hazardous_asteroid":false,
            "close_approach_data":[  
               {  
                  "close_approach_date":"2018-06-18",
                  "epoch_date_close_approach":1529305200000,
                  "relative_velocity":{  
                     "kilometers_per_second":"12.0438743406",
                     "kilometers_per_hour":"43357.9476263051",
                     "miles_per_hour":"26940.9425125783"
                  },
                  "miss_distance":{  
                     "astronomical":"0.3064664713",
                     "lunar":"119.2154541016",
                     "kilometers":"45846732",
                     "miles":"28487838"
                  },
                  "orbiting_body":"Earth"
               }
            ]
         },
         {  
            "links":{  
               "self":"https://api.nasa.gov/neo/rest/v1/neo/3563152?api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS"
            },
            "neo_reference_id":"3563152",
            "name":"(2011 GA62)",
            "nasa_jpl_url":"http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=3563152",
            "absolute_magnitude_h":18.9,
            "estimated_diameter":{  
               "kilometers":{  
                  "estimated_diameter_min":0.4411182,
                  "estimated_diameter_max":0.9863702813
               },
               "meters":{  
                  "estimated_diameter_min":441.1181999969,
                  "estimated_diameter_max":986.3702813054
               },
               "miles":{  
                  "estimated_diameter_min":0.2740980571,
                  "estimated_diameter_max":0.6129018881
               },
               "feet":{  
                  "estimated_diameter_min":1447.2382352778,
                  "estimated_diameter_max":3236.1230737181
               }
            },
            "is_potentially_hazardous_asteroid":false,
            "close_approach_data":[  
               {  
                  "close_approach_date":"2018-06-18",
                  "epoch_date_close_approach":1529305200000,
                  "relative_velocity":{  
                     "kilometers_per_second":"7.9455926818",
                     "kilometers_per_hour":"28604.1336544372",
                     "miles_per_hour":"17773.4962698918"
                  },
                  "miss_distance":{  
                     "astronomical":"0.1550728286",
                     "lunar":"60.3233299255",
                     "kilometers":"23198564",
                     "miles":"14414920"
                  },
                  "orbiting_body":"Earth"
               }
            ]
         },
         {  
            "links":{  
               "self":"https://api.nasa.gov/neo/rest/v1/neo/3733265?api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS"
            },
            "neo_reference_id":"3733265",
            "name":"(2015 VD2)",
            "nasa_jpl_url":"http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=3733265",
            "absolute_magnitude_h":24.6,
            "estimated_diameter":{  
               "kilometers":{  
                  "estimated_diameter_min":0.0319561887,
                  "estimated_diameter_max":0.0714562102
               },
               "meters":{  
                  "estimated_diameter_min":31.9561886721,
                  "estimated_diameter_max":71.4562101727
               },
               "miles":{  
                  "estimated_diameter_min":0.0198566489,
                  "estimated_diameter_max":0.0444008168
               },
               "feet":{  
                  "estimated_diameter_min":104.8431420431,
                  "estimated_diameter_max":234.436392583
               }
            },
            "is_potentially_hazardous_asteroid":false,
            "close_approach_data":[  
               {  
                  "close_approach_date":"2018-06-18",
                  "epoch_date_close_approach":1529305200000,
                  "relative_velocity":{  
                     "kilometers_per_second":"16.5811851502",
                     "kilometers_per_hour":"59692.2665406091",
                     "miles_per_hour":"37090.4530624135"
                  },
                  "miss_distance":{  
                     "astronomical":"0.3445160884",
                     "lunar":"134.0167694092",
                     "kilometers":"51538876",
                     "miles":"32024772"
                  },
                  "orbiting_body":"Earth"
               }
            ]
         }
      ],
      "2018-06-17":[  
         {  
            "links":{  
               "self":"https://api.nasa.gov/neo/rest/v1/neo/2162168?api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS"
            },
            "neo_reference_id":"2162168",
            "name":"162168 (1999 GT6)",
            "nasa_jpl_url":"http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=2162168",
            "absolute_magnitude_h":17.0,
            "estimated_diameter":{  
               "kilometers":{  
                  "estimated_diameter_min":1.0581688593,
                  "estimated_diameter_max":2.3661375011
               },
               "meters":{  
                  "estimated_diameter_min":1058.1688593312,
                  "estimated_diameter_max":2366.137501138
               },
               "miles":{  
                  "estimated_diameter_min":0.6575154423,
                  "estimated_diameter_max":1.4702492252
               },
               "feet":{  
                  "estimated_diameter_min":3471.6827204482,
                  "estimated_diameter_max":7762.9185592335
               }
            },
            "is_potentially_hazardous_asteroid":false,
            "close_approach_data":[  
               {  
                  "close_approach_date":"2018-06-17",
                  "epoch_date_close_approach":1529218800000,
                  "relative_velocity":{  
                     "kilometers_per_second":"9.6198699215",
                     "kilometers_per_hour":"34631.5317173891",
                     "miles_per_hour":"21518.6870273966"
                  },
                  "miss_distance":{  
                     "astronomical":"0.2630390442",
                     "lunar":"102.3221893311",
                     "kilometers":"39350084",
                     "miles":"24451008"
                  },
                  "orbiting_body":"Earth"
               }
            ]
         },
         {  
            "links":{  
               "self":"https://api.nasa.gov/neo/rest/v1/neo/3785933?api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS"
            },
            "neo_reference_id":"3785933",
            "name":"(2017 UA)",
            "nasa_jpl_url":"http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=3785933",
            "absolute_magnitude_h":28.5,
            "estimated_diameter":{  
               "kilometers":{  
                  "estimated_diameter_min":0.0053034072,
                  "estimated_diameter_max":0.0118587791
               },
               "meters":{  
                  "estimated_diameter_min":5.3034072332,
                  "estimated_diameter_max":11.8587790858
               },
               "miles":{  
                  "estimated_diameter_min":0.0032953835,
                  "estimated_diameter_max":0.0073687014
               },
               "feet":{  
                  "estimated_diameter_min":17.3996305869,
                  "estimated_diameter_max":38.9067567758
               }
            },
            "is_potentially_hazardous_asteroid":false,
            "close_approach_data":[  
               {  
                  "close_approach_date":"2018-06-17",
                  "epoch_date_close_approach":1529218800000,
                  "relative_velocity":{  
                     "kilometers_per_second":"7.3524855265",
                     "kilometers_per_hour":"26468.94789533",
                     "miles_per_hour":"16446.7748741844"
                  },
                  "miss_distance":{  
                     "astronomical":"0.3811914985",
                     "lunar":"148.283493042",
                     "kilometers":"57025436",
                     "miles":"35433964"
                  },
                  "orbiting_body":"Earth"
               }
            ]
         },
         {  
            "links":{  
               "self":"https://api.nasa.gov/neo/rest/v1/neo/3801999?api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS"
            },
            "neo_reference_id":"3801999",
            "name":"(2018 FS1)",
            "nasa_jpl_url":"http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=3801999",
            "absolute_magnitude_h":20.8,
            "estimated_diameter":{  
               "kilometers":{  
                  "estimated_diameter_min":0.1838886721,
                  "estimated_diameter_max":0.411187571
               },
               "meters":{  
                  "estimated_diameter_min":183.8886720703,
                  "estimated_diameter_max":411.1875710413
               },
               "miles":{  
                  "estimated_diameter_min":0.1142630881,
                  "estimated_diameter_max":0.2555000322
               },
               "feet":{  
                  "estimated_diameter_min":603.309310875,
                  "estimated_diameter_max":1349.040630575
               }
            },
            "is_potentially_hazardous_asteroid":false,
            "close_approach_data":[  
               {  
                  "close_approach_date":"2018-06-17",
                  "epoch_date_close_approach":1529218800000,
                  "relative_velocity":{  
                     "kilometers_per_second":"11.6654375478",
                     "kilometers_per_hour":"41995.5751720393",
                     "miles_per_hour":"26094.4172506485"
                  },
                  "miss_distance":{  
                     "astronomical":"0.3818340382",
                     "lunar":"148.5334320068",
                     "kilometers":"57121560",
                     "miles":"35493692"
                  },
                  "orbiting_body":"Earth"
               }
            ]
         }
      ],
      "2018-06-16":[  
         {  
            "links":{  
               "self":"https://api.nasa.gov/neo/rest/v1/neo/3279436?api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS"
            },
            "neo_reference_id":"3279436",
            "name":"(2005 LU3)",
            "nasa_jpl_url":"http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=3279436",
            "absolute_magnitude_h":26.2,
            "estimated_diameter":{  
               "kilometers":{  
                  "estimated_diameter_min":0.0152951935,
                  "estimated_diameter_max":0.0342010925
               },
               "meters":{  
                  "estimated_diameter_min":15.2951935344,
                  "estimated_diameter_max":34.201092472
               },
               "miles":{  
                  "estimated_diameter_min":0.0095039897,
                  "estimated_diameter_max":0.021251567
               },
               "feet":{  
                  "estimated_diameter_min":50.1810827555,
                  "estimated_diameter_max":112.2083122258
               }
            },
            "is_potentially_hazardous_asteroid":false,
            "close_approach_data":[  
               {  
                  "close_approach_date":"2018-06-16",
                  "epoch_date_close_approach":1529132400000,
                  "relative_velocity":{  
                     "kilometers_per_second":"16.9895489303",
                     "kilometers_per_hour":"61162.3761489486",
                     "miles_per_hour":"38003.9220021064"
                  },
                  "miss_distance":{  
                     "astronomical":"0.2620137139",
                     "lunar":"101.9233322144",
                     "kilometers":"39196692",
                     "miles":"24355696"
                  },
                  "orbiting_body":"Earth"
               }
            ]
         },
         {  
            "links":{  
               "self":"https://api.nasa.gov/neo/rest/v1/neo/3283835?api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS"
            },
            "neo_reference_id":"3283835",
            "name":"(2005 NW44)",
            "nasa_jpl_url":"http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=3283835",
            "absolute_magnitude_h":20.4,
            "estimated_diameter":{  
               "kilometers":{  
                  "estimated_diameter_min":0.2210828104,
                  "estimated_diameter_max":0.4943561926
               },
               "meters":{  
                  "estimated_diameter_min":221.0828103591,
                  "estimated_diameter_max":494.3561926196
               },
               "miles":{  
                  "estimated_diameter_min":0.137374447,
                  "estimated_diameter_max":0.3071786018
               },
               "feet":{  
                  "estimated_diameter_min":725.3373275385,
                  "estimated_diameter_max":1621.9035709942
               }
            },
            "is_potentially_hazardous_asteroid":false,
            "close_approach_data":[  
               {  
                  "close_approach_date":"2018-06-16",
                  "epoch_date_close_approach":1529132400000,
                  "relative_velocity":{  
                     "kilometers_per_second":"12.0629690223",
                     "kilometers_per_hour":"43426.6884801364",
                     "miles_per_hour":"26983.6554058936"
                  },
                  "miss_distance":{  
                     "astronomical":"0.0548729113",
                     "lunar":"21.3455619812",
                     "kilometers":"8208871",
                     "miles":"5100756"
                  },
                  "orbiting_body":"Earth"
               }
            ]
         },
         {  
            "links":{  
               "self":"https://api.nasa.gov/neo/rest/v1/neo/3395958?api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS"
            },
            "neo_reference_id":"3395958",
            "name":"(2007 WP3)",
            "nasa_jpl_url":"http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=3395958",
            "absolute_magnitude_h":23.5,
            "estimated_diameter":{  
               "kilometers":{  
                  "estimated_diameter_min":0.0530340723,
                  "estimated_diameter_max":0.1185877909
               },
               "meters":{  
                  "estimated_diameter_min":53.0340723319,
                  "estimated_diameter_max":118.5877908577
               },
               "miles":{  
                  "estimated_diameter_min":0.0329538346,
                  "estimated_diameter_max":0.0736870142
               },
               "feet":{  
                  "estimated_diameter_min":173.9963058693,
                  "estimated_diameter_max":389.0675677576
               }
            },
            "is_potentially_hazardous_asteroid":false,
            "close_approach_data":[  
               {  
                  "close_approach_date":"2018-06-16",
                  "epoch_date_close_approach":1529132400000,
                  "relative_velocity":{  
                     "kilometers_per_second":"15.7057349157",
                     "kilometers_per_hour":"56540.6456964908",
                     "miles_per_hour":"35132.1584329112"
                  },
                  "miss_distance":{  
                     "astronomical":"0.1746641507",
                     "lunar":"67.9443588257",
                     "kilometers":"26129386",
                     "miles":"16236048"
                  },
                  "orbiting_body":"Earth"
               }
            ]
         },
         {  
            "links":{  
               "self":"https://api.nasa.gov/neo/rest/v1/neo/3429684?api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS"
            },
            "neo_reference_id":"3429684",
            "name":"(2008 TZ)",
            "nasa_jpl_url":"http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=3429684",
            "absolute_magnitude_h":25.4,
            "estimated_diameter":{  
               "kilometers":{  
                  "estimated_diameter_min":0.022108281,
                  "estimated_diameter_max":0.0494356193
               },
               "meters":{  
                  "estimated_diameter_min":22.1082810359,
                  "estimated_diameter_max":49.435619262
               },
               "miles":{  
                  "estimated_diameter_min":0.0137374447,
                  "estimated_diameter_max":0.0307178602
               },
               "feet":{  
                  "estimated_diameter_min":72.5337327539,
                  "estimated_diameter_max":162.1903570994
               }
            },
            "is_potentially_hazardous_asteroid":false,
            "close_approach_data":[  
               {  
                  "close_approach_date":"2018-06-16",
                  "epoch_date_close_approach":1529132400000,
                  "relative_velocity":{  
                     "kilometers_per_second":"17.3250625132",
                     "kilometers_per_hour":"62370.2250473537",
                     "miles_per_hour":"38754.4323356737"
                  },
                  "miss_distance":{  
                     "astronomical":"0.1975837242",
                     "lunar":"76.8600692749",
                     "kilometers":"29558104",
                     "miles":"18366554"
                  },
                  "orbiting_body":"Earth"
               }
            ]
         },
         {  
            "links":{  
               "self":"https://api.nasa.gov/neo/rest/v1/neo/3667048?api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS"
            },
            "neo_reference_id":"3667048",
            "name":"(2014 GQ17)",
            "nasa_jpl_url":"http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=3667048",
            "absolute_magnitude_h":27.1,
            "estimated_diameter":{  
               "kilometers":{  
                  "estimated_diameter_min":0.0101054342,
                  "estimated_diameter_max":0.0225964377
               },
               "meters":{  
                  "estimated_diameter_min":10.1054341542,
                  "estimated_diameter_max":22.5964377109
               },
               "miles":{  
                  "estimated_diameter_min":0.0062792237,
                  "estimated_diameter_max":0.0140407711
               },
               "feet":{  
                  "estimated_diameter_min":33.1543125905,
                  "estimated_diameter_max":74.1352966996
               }
            },
            "is_potentially_hazardous_asteroid":false,
            "close_approach_data":[  
               {  
                  "close_approach_date":"2018-06-16",
                  "epoch_date_close_approach":1529132400000,
                  "relative_velocity":{  
                     "kilometers_per_second":"8.1155789012",
                     "kilometers_per_hour":"29216.0840443903",
                     "miles_per_hour":"18153.738444138"
                  },
                  "miss_distance":{  
                     "astronomical":"0.0855466094",
                     "lunar":"33.277633667",
                     "kilometers":"12797591",
                     "miles":"7952054.5"
                  },
                  "orbiting_body":"Earth"
               }
            ]
         },
         {  
            "links":{  
               "self":"https://api.nasa.gov/neo/rest/v1/neo/2469737?api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS"
            },
            "neo_reference_id":"2469737",
            "name":"469737 (2005 NW44)",
            "nasa_jpl_url":"http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=2469737",
            "absolute_magnitude_h":20.4,
            "estimated_diameter":{  
               "kilometers":{  
                  "estimated_diameter_min":0.2210828104,
                  "estimated_diameter_max":0.4943561926
               },
               "meters":{  
                  "estimated_diameter_min":221.0828103591,
                  "estimated_diameter_max":494.3561926196
               },
               "miles":{  
                  "estimated_diameter_min":0.137374447,
                  "estimated_diameter_max":0.3071786018
               },
               "feet":{  
                  "estimated_diameter_min":725.3373275385,
                  "estimated_diameter_max":1621.9035709942
               }
            },
            "is_potentially_hazardous_asteroid":false,
            "close_approach_data":[  
               {  
                  "close_approach_date":"2018-06-16",
                  "epoch_date_close_approach":1529132400000,
                  "relative_velocity":{  
                     "kilometers_per_second":"12.0629682412",
                     "kilometers_per_hour":"43426.685668464",
                     "miles_per_hour":"26983.6536588298"
                  },
                  "miss_distance":{  
                     "astronomical":"0.0548729224",
                     "lunar":"21.3455677032",
                     "kilometers":"8208872.5",
                     "miles":"5100757"
                  },
                  "orbiting_body":"Earth"
               }
            ]
         },
         {  
            "links":{  
               "self":"https://api.nasa.gov/neo/rest/v1/neo/3797700?api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS"
            },
            "neo_reference_id":"3797700",
            "name":"(2018 BC)",
            "nasa_jpl_url":"http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=3797700",
            "absolute_magnitude_h":29.4,
            "estimated_diameter":{  
               "kilometers":{  
                  "estimated_diameter_min":0.0035039264,
                  "estimated_diameter_max":0.0078350176
               },
               "meters":{  
                  "estimated_diameter_min":3.5039264111,
                  "estimated_diameter_max":7.8350176433
               },
               "miles":{  
                  "estimated_diameter_min":0.0021772383,
                  "estimated_diameter_max":0.0048684527
               },
               "feet":{  
                  "estimated_diameter_min":11.4958219265,
                  "estimated_diameter_max":25.705439285
               }
            },
            "is_potentially_hazardous_asteroid":false,
            "close_approach_data":[  
               {  
                  "close_approach_date":"2018-06-16",
                  "epoch_date_close_approach":1529132400000,
                  "relative_velocity":{  
                     "kilometers_per_second":"1.8772738596",
                     "kilometers_per_hour":"6758.1858945152",
                     "miles_per_hour":"4199.2738965114"
                  },
                  "miss_distance":{  
                     "astronomical":"0.0713602174",
                     "lunar":"27.7591247559",
                     "kilometers":"10675336",
                     "miles":"6633346.5"
                  },
                  "orbiting_body":"Earth"
               }
            ]
         }
      ]
   }
}
</script>

Possible to iterate through a JSON object and return values of each , Possible to iterate through a JSON object and return values of each property? keys of this somedata object, returned as an array Object.keys(somedata) I'm trying to accomplish, or do you think there's a more simple way? Arrays in JSON are almost the same as arrays in JavaScript. In JSON, array values must be of type string, number, object, array, boolean or null. In JavaScript, array values can be all of the above, plus any other valid JavaScript expression, including functions, dates, and undefined.

Try following

var day = "2018-06-09";
var result = {"links":{"next":"https://api.nasa.gov/neo/rest/v1/feed?start_date=2018-06-14&end_date=2018-06-21&detailed=false&api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS","prev":"https://api.nasa.gov/neo/rest/v1/feed?start_date=2018-05-31&end_date=2018-06-07&detailed=false&api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS","self":"https://api.nasa.gov/neo/rest/v1/feed?start_date=2018-06-07&end_date=2018-06-14&detailed=false&api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS"},"element_count":40,"near_earth_objects":{"2018-06-09":[{"links":{"self":"https://api.nasa.gov/neo/rest/v1/neo/2162474?api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS"},"neo_reference_id":"2162474","name":"162474 (2000 LB16)","nasa_jpl_url":"http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=2162474","absolute_magnitude_h":18.5,"estimated_diameter":{"kilometers":{"estimated_diameter_min":0.5303407233,"estimated_diameter_max":1.1858779086},"meters":{"estimated_diameter_min":530.3407233187,"estimated_diameter_max":1185.8779085771},"miles":{"estimated_diameter_min":0.3295383456,"estimated_diameter_max":0.7368701419},"feet":{"estimated_diameter_min":1739.963058693,"estimated_diameter_max":3890.675677576}},"is_potentially_hazardous_asteroid":true,"close_approach_data":[{"close_approach_date":"2018-06-09","epoch_date_close_approach":1528527600000,"relative_velocity":{"kilometers_per_second":"29.5006815383","kilometers_per_hour":"106202.4535379664","miles_per_hour":"65990.074533077"},"miss_distance":{"astronomical":"0.2778177136","lunar":"108.0710983276","kilometers":"41560940","miles":"25824772"},"orbiting_body":"Earth"}]}]}};

result.near_earth_objects[day].forEach(({name}, i) => document.getElementById('n'+(i+1)).innerHTML = name);
<div id="n1"></div>

How to loop through a complex JSON tree of objects and arrays in , keys(json) to get all the keys of that object dynamically. You can then get the type of the values (nested object, array, string, number, etc.) by using the typeof  Convert and Loop through JSON with PHP and JavaScript Arrays/Objects September 30, 2014 by Jonathan Suh If you’re working with JSON (JavaScript Object Notation) and either need to convert a JSON string to array or object and loop through it or vice-versa, take an array or object and convert it to a JSON string to return, both can be done in PHP or JavaScript.

Thanks everyone. Yes turns out I just needeed to do:

for (let i=0; i<result.near_earth_objects[today].length; i++) {

        let id = 'n'+(i+1);

        let output = document.getElementById(id);
        output.innerHTML = result.near_earth_objects[today][i].name;
    }

And thanks strah output.innerhtml was also causing an error...

How to loop through json response in javascript? - JavaScript, If you really want to loop through json, or any object, use a for(in. Arrays of objects are absolutely iterable. parse it and catch the result in a variable (on the assumption you want to do more than 1 thing with it). I didn't know they were parsing a response with the property containing a JSON array. (10 or more objects) ] } Eventually (it's very large!) the children array will contain objects without a children array. I need to loop through the whole object and get all the elements that have property content-kind: "Video"; if it has a content-kind property it is guaranteed not to have a children array. Sometimes what might happen is this:

I had to fake the api call, I didn't have a key. One thing I saw was that I needed to compare y with the actual date. This might just be a result of my mocking the returned result. Then the innerHTML, as you saw, had to be case corrected. FWIW, here's a working example.

<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Asteroids</title>

    <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body> 
    <div id="n1"></div> 
    <script>
        var today = "2018-06-09";
        var result = {
            "links": {
                "next": "https://api.nasa.gov/neo/rest/v1/feed?start_date=2018-06-14&end_date=2018-06-21&detailed=false&api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS",
                "prev": "https://api.nasa.gov/neo/rest/v1/feed?start_date=2018-05-31&end_date=2018-06-07&detailed=false&api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS",
                "self": "https://api.nasa.gov/neo/rest/v1/feed?start_date=2018-06-07&end_date=2018-06-14&detailed=false&api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS"
            },
            "element_count": 40,
            "near_earth_objects": {
                "2018-06-09": [{
                    "links": {
                        "self": "https://api.nasa.gov/neo/rest/v1/neo/2162474?api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS"
                    },
                    "neo_reference_id": "2162474",
                    "name": "162474 (2000 LB16)",
                    "nasa_jpl_url": "http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=2162474",
                    "absolute_magnitude_h": 18.5,
                    "estimated_diameter": {
                        "kilometers": {
                            "estimated_diameter_min": 0.5303407233,
                            "estimated_diameter_max": 1.1858779086
                        },
                        "meters": {
                            "estimated_diameter_min": 530.3407233187,
                            "estimated_diameter_max": 1185.8779085771
                        },
                        "miles": {
                            "estimated_diameter_min": 0.3295383456,
                            "estimated_diameter_max": 0.7368701419
                        },
                        "feet": {
                            "estimated_diameter_min": 1739.963058693,
                            "estimated_diameter_max": 3890.675677576
                        }
                    },
                    "is_potentially_hazardous_asteroid": true,
                    "close_approach_data": [{
                        "close_approach_date": "2018-06-09",
                        "epoch_date_close_approach": 1528527600000,
                        "relative_velocity": {
                            "kilometers_per_second": "29.5006815383",
                            "kilometers_per_hour": "106202.4535379664",
                            "miles_per_hour": "65990.074533077"
                        },
                        "miss_distance": {
                            "astronomical": "0.2778177136",
                            "lunar": "108.0710983276",
                            "kilometers": "41560940",
                            "miles": "25824772"
                        },
                        "orbiting_body": "Earth"
                    }]
                }]
            }
        };
        (function () { 
            // iterate over all keys
            for (let x in result) { 
                // if key is 'near_earth_objects'
                if (x == 'near_earth_objects') { 
                    // iterate over dates in 'near_earth_objects'
                    for (y in result.near_earth_objects) { 
                        // if todays date
                        if (y == today) { //Check y with today's value

                            // loop over array of objects for todays date
                            for (let i = 0; i < result.near_earth_objects[today].length; i++) {


                                let id = 'n' + (i + 1);

                                let output = document.getElementById(id);
                                output.innerHTML = result.near_earth_objects[today][i].name;
                            }
                        }
                    }
                }
            }

        })();
    </script> 
</body>
</html>

Iterate over a JSONObject?, We know how to iterate over JSONArrays, but when we parse JSON data from Facebook There is no more simple and secure solution than using an iterator. JSONObject objects = new JSONObject (); JSONArray key = objects.names Ho do I Iterate through a HashMap which contains duplicate values. Extract JSON array of numbers from JSON array of objects How to turn json array into postgres array? I was thinking of creating a plgpsql function but wasn't able to figure out . Any advice I would greatly appreciate it! Thank you!! I would also like to avoid CROSS JOIN LATERAL because it will slow down the performance.

JSON Arrays, Arrays as JSON Objects In JSON, array values must be of type string, number, object, array, boolean or You can access array values by using a for-in loop:  Looping through the array Once you’ve converted the object into an array with Object.keys, Object.values, or Object.entries, you can loop through it as if it was a normal array. If you use Object.entries you might want to destructure the array into its key and property.

Looping JavaScript Arrays Using for, forEach & More ‍ , There are different ways to loop over arrays in JavaScript, but it can be As the language has matured so have our options to loop over arrays and objects. A node list is not an array, but an enumeration object, containing  The clean answer is that you know, precisely, the structure of that JSON before you begin to read it. That allows you to know where the nested objects/keys are and where the arrays are to loop over them, etc.

JSON Array – REST API Tutorial, JSON array can store string, number, boolean, object or other array inside JSON array. In JSON array You can access array values by using a for-in loop:  Often, we manipulate JSON objects and arrays. Hence, it becomes essential to know how to loop through an object or an array so that we can show the data accordingly. In this blog, we will discuss

Comments
  • Any console errors? Make sure first you're even getting to the innermost loop by adding a console.log(id) somewhere in there, then seeing if this outputs in the console next time you run this function
  • Are you sure you have any divs in your DOM with id = "n1", "n2", etc. ?
  • It should be output.innerHTML, just sayin'
  • Thanks Ivan. Yes I believe I will use the createElement method now. Will this allow me to dynamically add tables to my html page at runtime? My objective is to have a table for each asteroid with name, mass, velocity etc. however the number of asteroids and therefore tables will be different each day
  • Of course this can be done client side with JavaScript
  • Apart from correcting the mistake in your code, an important aspect is to get the appropriate code as well. In the code that you have shared in question is not the right way to do it. There are unnecessary loops which should be avoided.
  • Yep I literally didn't need any of the previous loops. I removed those as well as correcting the mistake. Appreciate you all taking the time to help me. First question here, what a great community!