Allows you to convert a JavaScript object into JSON string.
This filter is mostly useful for debugging. When using the double curly {{value}} notation the binding is automatically converted to JSON.
{{ json_expression | json : spacing}}
$filter('json')(object, spacing)
Param | Type | Details |
---|---|---|
object | * |
Any JavaScript object (including arrays and primitive types) to filter. |
spacing
(optional)
|
number |
The number of spaces to use per indentation, defaults to 2. |
string | JSON string. |
<pre id="default-spacing">{{ {'name':'value'} | json }}</pre>
<pre id="custom-spacing">{{ {'name':'value'} | json:4 }}</pre>
it('should jsonify filtered objects', function() {
expect(element(by.id('default-spacing')).getText()).toMatch(/\{\n "name": ?"value"\n}/);
expect(element(by.id('custom-spacing')).getText()).toMatch(/\{\n "name": ?"value"\n}/);
});