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:
- var mySource = [
- {
- name: 'Jignesh Trivedi',
- age: '31'
- },
- {
- name: 'Tejas Trivedi',
- age: '33'
- }]
- var data = angular.toJson(mySource);
Output:
- [{
- "name": "Jignesh Trivedi",
- "age": "31"
- },
- {
- "name": "Tejas Trivedi",
- "age": "33"
- }]