Improve this Doc View Source angular.toJson

From Get docs
< Function components in ngAngularjs/docs/1.8/api/ng/function/angular.tojson

Improve this Doc View Source angular.toJson

  1. function in module ng


Serializes input into a JSON-formatted string. Properties with leading $$ characters will be stripped since AngularJS uses this notation internally.

Known Issues

The Safari browser throws a RangeError instead of returning null when it tries to stringify a Date object with an invalid date value. The only reliable way to prevent this is to monkeypatch the Date.prototype.toJSON method as follows:

var _DatetoJSON = Date.prototype.toJSON;
Date.prototype.toJSON = function() {
  try {
  } catch(e) {
    if (e instanceof RangeError) {
      return null;
    throw e;

See for more information.


angular.toJson(obj, pretty);


Param Type Details
obj ObjectArrayDatestringnumberboolean Input to be serialized into JSON.




If set to true, the JSON output will contain newlines and whitespace. If set to an integer, the JSON output will contain that many spaces per indentation.

(default: 2)


stringundefined JSON-ified string representing obj.

© 2010–2020 Google, Inc.
Licensed under the Creative Commons Attribution License 3.0.