Liondancer
Liondancer

Reputation: 16469

Appending objects to JSON lists

There is an exception occurring and I am not sure why because I am new to JS. As soon as I reach the line testData["beacons"].append(beacon); My code jumps to catch(e). I am assuming I cant append objects to other arrays?

JS:

$(document).ready(function() {
    // Data to describe what kind of test
    var testData = {
        "timestamp": "",
        "hive": 0,
        "hdfs": 0,
        // Contains a list of testData objects
        "beacons":[]
    };


    var testRun = document.getElementById("test-form");
    testRun.addEventListener('submit', function(event) {
        event.preventDefault();
        var selectedTest = document.querySelector('input[name=test-select]:checked');
        alert(selectedTest);
        var testType = selectedTest.id;
        if (testType == "hdfs-test") {
            testData["hdfs"] = 1;
            testData["hive"] = 0;
        } else if (testType == "hive-test") {
            testData["hdfs"] = 0;
            testData["hive"] = 1;
        } else if (testType == "hdfs-hive-test") {
            testData["hdfs"] = 1;
            testData["hive"] = 1;
        } else {
        //    null
        }

        var events = document.getElementById("event-textarea").value;
        // check in valid input
        var eventSource = events.replace("],[","],,,,[");
        // beaconLists allows users to submit --> [{beacon1}, {beacon2}, ...], [{beacon3}, {beacon4}, ...]
        var beaconLists = eventSource.split(",,,,");
        for (var i = 0; i < beaconLists.length; i++) {
            // inspect one list in beaconLists [{beacon1}, {beacon2}, ...]
            var beaconList = beaconLists[i];
            try {
                // list of JSON objects
                var beaconObjList = JSON.parse(beaconList);
                for (var j = 0; j < beaconObjList.length; j++) {
                    var beaconObj = beaconObjList[j];
                    if (beaconObj["data"] && beaconObj["application"]) {
                    //    successful parse to find events
                    //    describe beacon being tested
                        alert("yes");
                        var beacon = {
                            "app_name": beaconObj["application"]["app_name"],
                            "device": beaconObj["application"]["device"],
                            "device_id": beaconObj["application"]["device_id"],
                            "os": beaconObj["application"]["os"],
                            "os_version": beaconObj["application"]["os_version"],
                            "browser": beaconObj["application"]["browser"],
                            "beacon": beaconObj
                        };
                        // append to testData
                        testData["beacons"].append(beacon);
                        // reset beacon so we can append new beacon later
                        beacon = {};
                    } else {
                    //    notify event isn't in the correct format?
                        alert("no");
                    }
                }
            } catch (e) {
            //     notify bad JSON
                alert("failed");
            }
        }
        console.log(testData);
        //$.ajax({
        //    type: "POST",
        //    url: "/test/",
        //    data: testData,
        //    success: function () {
        //        alert("yay");
        //    },
        //    failure: function () {
        //        alert("boo");
        //    }
        //});

    });

});

Upvotes: 1

Views: 73

Answers (2)

Everton Lenger
Everton Lenger

Reputation: 1446

You should use the push method, like:

testData["beacons"].push(beacon);

Upvotes: 1

Travis J
Travis J

Reputation: 82267

There is nothing wrong with having an array of objects. JavaScript handles that just fine. The main issue is that append is a jQuery API method for adding elements (psuedo native appendChild). push is how you add to an array.

testData["beacons"].push(beacon);

Further, this part of your code is problematic.

// reset beacon so we can append new beacon later
beacon = {};

Both the variable beacon and the one added here testData["beacons"] are the same. In JavaScript, the value of testData["beacons"]'s recent beacon is the same as the variable beacon. When the value in the variable beacon is set to {}, so is the array's value. This line of code simply needs to be removed. Inside of the variable environment set up, the use of var will set up a new variable for beacon each iteration.

Upvotes: 2

Related Questions