AngularJS | json Filter


The json filter in AngularJs is used to convert a JavaScript object into a JSON. string.JavaScript object that we are using can be of any kind of JavaScript Object.

Syntax:

    {{ object | json : spacing }}

Where json is used to specifies that object should be displayed in JSON format and spacing is an optional parameter with default value 2 that specifies the number of spaces per indentation.



Example 1:
This example will display the marks of student in JSON

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
<script src=
</script>
    </head>
<body>
  
<div ng-app="result" ng-controller="resultCtrl">
  
<h1>Result:</h1>
  
<pre>{{marks | json}}</pre>
  
</div>
  
<script>
var app = angular.module('result', []);
app.controller('resultCtrl', function($scope) {
$scope.marks = { 
        "Math" : 98,
        "Computer" : 93,
        "Physics" : 95,
        "Chemistry" : 95,
        "English" : 74
    };
});
</script>
  
</body>
</html>

chevron_right


Output :

output_img

Example 2:
This example will display fruits name in JSON with 10 spaces per indentation

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<head>
<script src=
</script>
</head>
<body>
  
<div ng-app="fruit" ng-controller="fruitCtrl">
  
<h1>Fruits:</h1>
  
<pre>{{fruit | json : 10}}</pre>
  
</div>
  
<script>
var app = angular.module('fruit', []);
app.controller('fruitCtrl', function($scope) {
    $scope.fruit = ["Apple", "Banana", "Mango"];
});
</script>
  
</body>
</html>

chevron_right


Output :



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.