Using JSON JavaScript object for serialization and de-serialization

Now most of the browser’s support JSON object for serializing javascript object array to JSON format and parsing JSON string.

For using this you have to use like this JSON.[method]

But before using inbuilt JSON object of any browser you should check in you code that current browser support this or not.

if (!this.JSON) {
//Not supported

Currently it supports only two function

JSON.parse: This function used to parse JSON string. This method takes string as first argument and optionally second argument a function for doing costume filtering and modification.

Below i have written two example for parsing JSON string.

In second example of JSONParse method i called makeUpper method as second argument which will make firstname and surname field to upper case.
JSON.parse Eample:

    function JSONParse() {
        var jsonstring = '{"firstname":"Sheel","surname":"Prabhakar",
        var contact = JSON.parse(jsonstring);
        var fullname = contact.surname + ", " + contact.firstname;
        // The value of fullname is "Aaberg, Jesper"
function JSONParse() {
var jsonstring = '{"firstname":"Sheel","surname":"Prabhakar",
var contact = JSON.parse(jsonstring, makeUpper);
var fullname = contact.surname + ", " + contact.firstname;
// The value of fullname is "Aaberg, Jesper"

function makeUpper(key, value) {
if (typeof value === 'string' && key !== 'phone') {
return value.toUpperCase();
return value;

JSON.stringify: This function used to serialize objects to JSON string. This function takes first argument java script array object to serialize and second optional param a function or array to filter and transform the result and third optional argument for Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.

function makeJSON() {
var numbers = new Array();
for (var i = 0; i < 20; i++) {
numbers[i] = i;
var jsonstring = JSON.stringify(numbers);
//Output is given here

In above function it serializes simple array. Here is example for array of object with spacer.

function makeJSON() {
var contact = new Object();
contact.firstname = "Jesper";
contact.surname = "Aaberg"; = ["555-0100", "555-0120"];

var memberfilter = new Array();
memberfilter[0] = "surname";
memberfilter[1] = "phone";
var jsonText = JSON.stringify(contact, memberfilter, "t");

/* The value of jsonText is:
        "surname": "Aaberg",
        "phone": [

In above example there is one filter for omitting first name and third param for tab spacing.
In third param you can use following values

  • Quotation mark (“)
  • Backslash ()
  • Backspace (b)
  • Formfeed (f)
  • Newline (n)
  • Carriage return (r)
  • Horizontal tab (t)
  • Four-hexadecimal-digits (uhhhh)

Leave a Reply

Your email address will not be published. Required fields are marked *