Serializes Input into a JSON-Formatted in AngularJS

Angular function toJson used to serialize input into a JSON formatted string.
 
Syntax:

angular.toJson(obj, pretty);
 
Arguments:

obj: Input to be serialized into JSON.
pretty: It is optional parameter. It is boolean / number type. If it is set to true, the JSON output will contains white-space and newline character. if it is set to an integer, the JSON output will contains many spaces per indentation.
 
Example:
  1. var mySource = [  
  2. {  
  3.     name: 'Jignesh Trivedi',  
  4.     age: '31'  
  5. },  
  6. {  
  7.     name: 'Tejas Trivedi',  
  8.     age: '33'  
  9. }]  
  10. var data = angular.toJson(mySource);  
Output:
  1. [{  
  2.     "name""Jignesh Trivedi",  
  3.     "age""31"  
  4. },  
  5. {  
  6.     "name""Tejas Trivedi",  
  7.     "age""33"  
  8. }]  
Ebook Download
View all
Learn
View all