AngularJS angular.toJson() Function (original) (raw)
Last Updated : 24 Jul, 2023
The angular.toJson() Function in AngularJS is used to serialize the javascript object into a JSON - formatted string. It takes the javascript object and returns a JSON string.
The angular.toJson() function in AngularJS is capable of handling circular references in JavaScript objects. Circular references occur when an object refers to itself or when two or more objects refer to each other in a chain. Unlike the native JSON.stringify() method, which throws errors when encountering circular references, the angular.toJson() internally handles such scenarios. It replaces circular references with the placeholder string [Circular], allowing the serialization process to proceed without interruptions. This capability is particularly useful when working with complex object structures that may contain circular references, ensuring reliable and error-free JSON serialization within AngularJS applications.
Syntax:
angular.toJson(object);
Parameter:
- object: It specifies the input will be serialized into the JSON.
Return Type:
- It returns JSON-ified string that represents the object.
Example 1: This example describes the use of the angular.toJson() Function in AngularJS, where tag is utilized to group the related elements in the form, and it creates the box over the elements, in order to serialize the javascript object into a JSON string format.
HTML `
AngularJS toJSON() function