Knockout toJSON example on root
HTML
<script src="http://knockoutjs.com/downloads/knockout-2.2.1.js"></script>
<div id="div1" data-bind="text: ko.toJSON($root)"></div>
<div id="div2" data-bind="text: ko.toJSON($root)"></div>
JavaScript
var model = (function () {
var firstName = ko.observable("John"),
middleName = ko.observable('2222'), //will be serialized because a value is assigned.
lastName = ko.observableArray([1]) //won't be serialized because it has no value.
return {
firstName: firstName,
middleName: middleName,
lastName: lastName
}
})();
ko.applyBindings(model, document.getElementById('div1'));
var model2 = {
firstName: ko.observable("Bob"),
middleName: ko.observable(""), //will be serialized because a value is assigned.
lastName: ko.observable(null) //won't be serialized because it has no value.
}
ko.applyBindings(model2, document.getElementById('div2'));