0

I'm loading JSON from a url, I would like to create a loop through the subjects Array in the try and output each itemName. However it keeps erroring out, I'm fairly new to working with both JavaScript and JSON so I might be going about this the wrong way. Any advice would be greatly appreciated!

JavaScript code

<script>
        getUrl();
            
        //Get the Url with current date + current time
        function getUrl() {

        var dateObj = new Date();
        var month = ("0" + (dateObj.getMonth() + 1)).slice(-2);
        var day = ("0" + dateObj.getDate()).slice(-2);
        var year = dateObj.getFullYear();
        var hours = dateObj.getHours();
        var minutes = dateObj.getMinutes();
        var seconds = dateObj.getSeconds();

        var currentdate = "";
        currentdate = year + "-" + month + "-" + day + "T" + hours + ":" + minutes + ":" + seconds;

        var currenttime = "";
        currenttime = hours + minutes + seconds;

        var url = "http://snowautomation.msbb.uc.edu/API/schedule/getAval.sh?" + currentdate;
        console.log(url);
        loadJSON(url);   
            
        }
            
        //Load the JSON from the url
        function loadJSON(url) {
            
            fetch(url)
                .then(res => res.json())
                .then((out) => {
                parseData(out);
                })
                .catch(err => { throw err });
        }
        
            
        function parseData(json) {

            try {
                for (var i = 0; i < results.length; i++) {
                    for (var prop in result[i]) {
                        if (result[i].itemName[enter image description here][1](prop)) {
                            alert(result[i][prop]);
                        }
                    }
                }
            
                console.log(json);
        
            }
            catch {
                console.log("error");
            }
        }   
</script>
        

JSON

{comptype: "availability", headers: {…}, obj_id: 347, last_id: 28, subjects: Array(22), …}
comptype: "availability"
headers: {data: Array(17)}
last_id: 28
lastupdate: "2020-08-04T10:41:34.463"
obj_id: 347
page_count: 1
subjects: Array(22)
0: {itemId: 2305, itemName: "LINDHALL 0040", item_date: "2020-08-04T00:00:00", itemTypeId: 4, isFav: 0, …}
1: {itemId: 2303, itemName: "LINDHALL 0050", item_date: "2020-08-04T00:00:00", itemTypeId: 4, isFav: 0}
2: {itemId: 2304, itemName: "LINDHALL 0060", item_date: "2020-08-04T00:00:00", itemTypeId: 4, isFav: 0, …}
3: {itemId: 2306, itemName: "LINDHALL 0070", item_date: "2020-08-04T00:00:00", itemTypeId: 4, isFav: 0}
4: {itemId: 2340, itemName: "LINDHALL 1210", item_date: "2020-08-04T00:00:00", itemTypeId: 4, isFav: 0}
5: {itemId: 2307, itemName: "LINDHALL 1215", item_date: "2020-08-04T00:00:00", itemTypeId: 4, isFav: 0, …}
6: {itemId: 2309, itemName: "LINDHALL 2120", item_date: "2020-08-04T00:00:00", itemTypeId: 4, isFav: 0}
7: {itemId: 2310, itemName: "LINDHALL 2125", item_date: "2020-08-04T00:00:00", itemTypeId: 4, isFav: 0}
8: {itemId: 2311, itemName: "LINDHALL 2240", item_date: "2020-08-04T00:00:00", itemTypeId: 4, isFav: 0}
9: {itemId: 2312, itemName: "LINDHALL 2245", item_date: "2020-08-04T00:00:00", itemTypeId: 4, isFav: 0}
10: {itemId: 2313, itemName: "LINDHALL 2250", item_date: "2020-08-04T00:00:00", itemTypeId: 4, isFav: 0}
11: {itemId: 2314, itemName: "LINDHALL 3115", item_date: "2020-08-04T00:00:00", itemTypeId: 4, isFav: 0}
12: {itemId: 2315, itemName: "LINDHALL 3125", item_date: "2020-08-04T00:00:00", itemTypeId: 4, isFav: 0}
13: {itemId: 2316, itemName: "LINDHALL 3220", item_date: "2020-08-04T00:00:00", itemTypeId: 4, isFav: 0}
14: {itemId: 2317, itemName: "LINDHALL 3225", item_date: "2020-08-04T00:00:00", itemTypeId: 4, isFav: 0}
15: {itemId: 2318, itemName: "LINDHALL 3230", item_date: "2020-08-04T00:00:00", itemTypeId: 4, isFav: 0}
16: {itemId: 2319, itemName: "LINDHALL 3240", item_date: "2020-08-04T00:00:00", itemTypeId: 4, isFav: 0}
17: {itemId: 2320, itemName: "LINDHALL 3260", item_date: "2020-08-04T00:00:00", itemTypeId: 4, isFav: 0}
18: {itemId: 2321, itemName: "LINDHALL 3265", item_date: "2020-08-04T00:00:00", itemTypeId: 4, isFav: 0}
19: {itemId: 2322, itemName: "LINDHALL 4210", item_date: "2020-08-04T00:00:00", itemTypeId: 4, isFav: 0}
20: {itemId: 2323, itemName: "LINDHALL 4225", item_date: "2020-08-04T00:00:00", itemTypeId: 4, isFav: 0}
21: {itemId: 2324, itemName: "LINDHALL 4230", item_date: "2020-08-04T00:00:00", itemTypeId: 4, isFav: 0}
length: 22
__proto__: Array(0)
__proto__: Object
  • What do you mean by "set to a variable"? – tdranv Aug 04 '20 at 14:57
  • What do you mean by "Loop through Json array"? Your code doesn't make even any attempt to loop, even in pseudocode. – Buh Buh Aug 04 '20 at 14:59
  • 1
    The value you pass to `parseData()` isn't JSON anymore. It's an object. – Ivar Aug 04 '20 at 15:01
  • When I mean set to a variable I was referring to this example https://stackoverflow.com/questions/18238173/javascript-loop-through-json-array – bearcat4444 Aug 04 '20 at 15:11
  • @bearcat4444 In that example you could use `json[i].id`. That variable is not necessary. – Ivar Aug 04 '20 at 15:37
  • @Ivar I've changed my code to `function parseData(json) { try { for(var i = 0; i < json.length; i++) { var obj = json[i]; console.log(obj.itemName); } console.log(json); } catch { console.log("error"); } } ` But its not outputting anything new to the console – bearcat4444 Aug 04 '20 at 15:49
  • @bearcat4444 `json` isn't an array. The array you are looking for (presumably) is under `json.subjects`. – Ivar Aug 04 '20 at 16:07

0 Answers0