Skip to content
Search

JavaScript:JSON

lastmod 2025-08-06created 2020-10-192 edits

Methods

JSON.stringify

The JSON.stringify() method converts a JavaScript value or object to a JSON string. Optionally, if replacer is passed as a function, it can transform values before conversion; if passed as an array, only the specified properties are included in the result.

toJSON

If an object has a toJSON property (or function), it can override the JSON stringification behavior. For example:

var obj = {
  foo: 'foo',
  toJSON: function() {
    return 'bar';
  }
};
JSON.stringify(obj);        // '"bar"'
JSON.stringify({ x: obj }); // '{"x":"bar"}'

See also

author Zer0 sitemap