I was wondering how can I create a JSON (JS) object and then clone it.
Q1: How to create a JSON object in javascript/jquery?
Creating a Javascript object is so simple:
var user = {}; // creates an empty user object
var user = {firstName:"John", lastName:"Doe"}; // creates a user by initializing
// its firstName and lastName properties.
After the creation you can add extra fields to your object like user.age = 30;.
If you have the object as a JSON string, you can convert it to a JSON object using built-in JSON.parse(yourJsonString) function or jQuery's $.parseJSON(yourJsonString) function.
Q2: How do I clone a JSON object in javascript/jquery?
My way to clone JSON objects is extend function of jQuery. For example, you can generate a clone of your user object like below:
var cloneUser = $.extend(true, {}, {firstName:"John", lastName:"Doe"});
The first parameter designates whether the clone object will be a shallow or deep copy of the original (see Object copy on wiki).
To see other JSON cloning alternatives you can read this article.