A common use of JSON is to send data to a web server.
When sending data to a web server, it must be in string format.
For example, consider the following object in JavaScript:
var obj = {name:“John”, age:30, city:“New York”}; |
Use the JavaScript function JSON.stringify() to convert the object into a string.
var myJSON = JSON.stringify(obj); |
myJSON is now a string and can be sent to the server.
var obj = {name:“John”, age:30, city:“New York”}; var myJSON = JSON.stringify(obj); document.getElementById(“demo”).innerHTML = myJSON; |
Date.now()
returns the number of milliseconds elapsed since the “epoch” (January 1, 1970, 00:00:00 UTC).
var timInMSs = Date.now(); |
Date.now() returns the same value as calling getTime() on a Date object.
The toISOString() method converts a Date object into a string, formatted according to the ISO standard.
const d = new Date(); document.getElementById(“demo”).innerHTML = d.toISOString(); |
The toJSON() method converts a Date object into a string formatted as a JSON date, which follows the ISO-8601 standard: YYYY-MM-DDTHH:mm:ss.sssZ.
d = new Date(); document.getElementById(“demo”).innerHTML = d.toJSON(); |
ES5 allows you to define object methods using a syntax that resembles property access.
In this example, a getter is created for a property called fullName
:
// Create an object: var person = { firstName: “John”, lastName : “Doe”, get fullName() { return this.firstName + ” “ + this.lastName; } }; // Display data from the object using a getter: document.getElementById(“demo”).innerHTML = person.fullName; |
This example defines both a setter and a getter for the language
property:
var person = { firstName: “John”, lastName : “Doe”, language : “NO”, get lang() { return this.language; }, set lang(value) { this.language = value; } }; // Set an object property using a setter: person.lang = “en”; // Display data from the object using a getter: document.getElementById(“demo”).innerHTML = person.lang; |
This example uses a setter to ensure that updates to the language property are always in uppercase.
var person = { firstName: “John”, lastName : “Doe”, language : “NO”, set lang(value) { this.language = value.toUpperCase(); } }; // Set an object property using a setter: person.lang = “en”; // Display data from the object: document.getElementById(“demo”).innerHTML = person.language; |
Object.defineProperty() is a new method introduced in ES5 that allows you to define an object property and/or modify its value and metadata.
// Create an Object: const person = { firstName: “John”, lastName : “Doe”, language : “NO”, }; // Change a Property: Object.defineProperty(person, “language”, { value: “EN”, writable : true, enumerable : true, configurable : true }); // Enumerate Properties let txt = “”; for (let x in person) { txt += person[x] + “<br>”; } // Display Properties document.getElementById(“demo”).innerHTML = txt; |
The following example is the same code, but it hides the language property from enumeration.
// Create an Object: const person = { firstName: “John”, lastName : “Doe”, language : “NO”, }; // Change a Property: Object.defineProperty(person, “language”, { value: “EN”, writable : true, enumerable : false, configurable : true }); // Enumerate Properties let txt = “”; for (let x in person) { txt += person[x] + “<br>”; } document.getElementById(“demo”).innerHTML = txt; |
This example defines a setter and a getter to ensure that updates to the language property are always in uppercase.
// Create an Object: const person = { firstName: “John”, lastName : “Doe”, language : “NO” }; // Change a Property: Object.defineProperty(person, “language”, { get : function() { return language }, set : function(value) { language = value.toUpperCase()} }); // Change Language person.language = “en”; // Display Language document.getElementById(“demo”).innerHTML = person.language; |