# Function toJson

- toJson(obj, pretty?): string

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

  #### Parameters

  - obj: any

    Input to be serialized into JSON.

  - `Optional` pretty: number | boolean

    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.

  #### Returns string

  JSON-ified string representing `obj`.

  #### Known Issue

  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:

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

  See https://github.com/angular/angular.js/pull/14221 for more information.

  - Defined in shared/utils.js:848