Serialization

In this chapter you will learn:

  1. How to use JSON.stringify() to convert object to JSON string
  2. How to control the string indention in JSON
  3. How to indent JSON string value during serialization
  4. How to use toJSON() Method

JSON.stringify()

The JSON.stringify() method accepts two arguments in addition to the object to serialize.

The first argument is a filter, which can be either an array or a function. The second argument is an option for indenting the resulting JSON string.

If the argument is an array, JSON.stringify() includes only object properties that are listed in the array.

<!DOCTYPE html><!--from  ja v a2s. co m-->
<html>
<head>
    <script type="text/javascript">
        var book = { 
            "title": "JavaScript",
            "authors": ["J" ], 
             edition: 3, 
             year: 2011 
        }; 
        
        var jsonText = JSON.stringify(book, ["title", "edition"]); 
        document.writeln(jsonText);
    </script>

</head>
<body>
  
</body>
</html>

Click to view the demo

When the second argument is a function. The provided function receives two arguments: the property key name and the property value.

You can look at the key to determine what to do with the property. The key may be an empty string if a value isn't part of a key-value pair.

The undefined property is omitted from the result in the JSON.stringify method.

<!DOCTYPE html><!--from j a  v  a  2  s  .  c  o  m-->
<html>
<head>
    <script type="text/javascript">
        var book = {
         "title": "JavaScript",
         "authors": ["J"], 
          edition: 3, 
          year: 2011 
        }; 
        
        var jsonText = JSON.stringify(book, function(key, value){ 
            switch(key){ 
               case "authors": 
                  return value.join(",")
               case "year": 
                  return 2012;
               case "edition": 
                  return undefined;
               default: 
               return value; 
            } 
        }); 
        document.writeln(jsonText);
    </script>

</head>
<body>
  
</body>
</html>

Click to view the demo

String Indentation

The third argument of JSON.stringify() controls indentation and white space. When this argument is a number, it represents the number of spaces to indent each level.

The following code indents each level by four spaces:

<!DOCTYPE html><!--from  java2s. c om-->
<html>
<head>
    <script type="text/javascript">
            var book = {
             "title": "JavaScript",
             "authors": [ "J" ], 
              edition: 3, 
             year: 2011 
            }; 
            
            var jsonText = JSON.stringify(book, null, 4); 
            document.writeln("<pre>"+jsonText+"</pre>");
    </script>

</head>
<body>
  
</body>
</html>

Click to view the demo

Indent with string value

If the indentation argument is a string, the string is used as the indentation character for the JSON string instead of a space.

<!DOCTYPE html><!--from j  a  v a 2  s.com-->
<html>
<head>
    <script type="text/javascript">
            var book = {
             "title": "JavaScript",
             "authors": [ "J" ], 
              edition: 3, 
             year: 2011 
            }; 
            
            
            var jsonText = JSON.stringify(book, null, " - -"); 
            document.writeln("<pre>"+jsonText+"</pre>");
    </script>
</head>
<body>
</body>
</html>

Click to view the demo

toJSON() Method

You can add a toJSON() method to the object and have it return the proper JSON representation. The native Date object has a toJSON() method that converts JavaScript Date objects into an ISO 8601 date string.

A toJSON() method can be added to any object, for example:

<!DOCTYPE html><!--from   j a  va2 s . c  o m-->
<html>
<head>
    <script type="text/javascript">
            var book = { 
                "title": "JavaScript",
                "authors": ["J"],
                 edition: 3,
                 year: 2011,
                 toJSON: function(){ 
                    return this.title; 
                 }
            }; 
            
            var jsonText = JSON.stringify(book); 
            document.writeln("<pre>"+jsonText+"</pre>");
    </script>

</head>
<body>
  
</body>
</html>

Click to view the demo

Next chapter...

What you will learn in the next chapter:

  1. Regular Expressions in Javascript
Home » Javascript Tutorial » JSON
JSON
Parsing
Serialization