Passing nested Form Data to JSON ready for save

Passing nested Form Data to JSON ready for save

by Andrew Pougher

HTML

<link rel="stylesheet" href="http://code.jquery.com/ui/1.8.19/themes/base/jquery-ui.css">
<script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.14/jquery-ui.min.js"></script>
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.1/css/bootstrap-combined.min.css">
<input id="x" value="testing">
    <div id="emailsubscribe">
</div>
     
     <button id="save"  class='btn btn-large btn-success'>SAVE</button>
<div class="well"><code id="code1"></code></div><hr>
<code id="code2"></code>

JavaScript

var usersettings = {
	"CalendarFeedFlights": "http:\/\/www.focusdashboard.com\/feeds\/calendars\/3ca79d41-e71f-4c4d-9e16-d7ea40e50b69-flights.ics",
	"CalendarFeedFull": "http:\/\/www.focusdashboard.com\/feeds\/calendars\/3ca79d41-e71f-4c4d-9e16-d7ea40e50b69.ics",
	"CalendarFeedTrips": "http:\/\/www.focusdashboard.com\/feeds\/calendars\/3ca79d41-e71f-4c4d-9e16-d7ea40e50b69-trips.ics",
	"EmailAlerts": [
		{
			"AlertName": "emailalertaddhotel",
			"AlertText": "A hotel has been added to the booking",
			"SavedValue": false
		}, {
			"AlertName": "emailalertaddcar",
			"AlertText": "A car has been added to the booking",
			"SavedValue": false
		}, {
			"AlertName": "emailalertxxlhotel",
			"AlertText": "A hotel has been removed from the booking",
			"SavedValue": true
		}, {
			"AlertName": "emailalertxxlcar",
			"AlertText": "A car has been removed from the booking",
			"SavedValue": true
		}, {
			"AlertName": "emailalertretfltreminder",
			"AlertText": "Reminder for Return Flight",
			"SavedValue": false
		}, {
			"AlertName": "emailalertcheckinreminder",
			"AlertText": "Reminder for flight check in",
			"SavedValue": true
		}
	],
	"GUID": "3ca79d41-e71f-4c4d-9e16-d7ea40e50b69",
	"NumberBookingsToShow": 5
}

$.each(usersettings.EmailAlerts, function (i, v) {
        $("#emailsubscribe").append($("<label>").text(" " + this.AlertText).prepend(
            $("<input>").attr({
                'data-nom': this.AlertName+"|"+this.AlertText,
                'type': 'checkbox', 
                'class' : 'inp-checkbox', 
                'name':'subscribe',
            }).val(this.id)
               .prop('checked', this.SavedValue)
        ));
    });

// Created the JSON we need to insert into the Master JSON send back for update, making the nested object ready.
function createJSON() {
    jsonObj = [];
    $("#emailsubscribe input[name=subscribe]").each(function() {
        var alrtName = $(this).data('nom').split('|')[0];
        var alerttxt =...