You can use JSON.stringify with a replacer like:

JSON.stringify({
   color: 'red',
   doSomething: function (arg) {
        alert('Do someting called with ' + arg);
   }
}, function(key, val) {
        return (typeof val === 'function') ? '' + val : val;
});
Answer from CD.. on Stack Overflow
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ JSON โ€บ stringify
JSON.stringify() - JavaScript | MDN - Mozilla
The JSON.stringify() static method converts a JavaScript value to a JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified.
๐ŸŒ
W3Schools
w3schools.com โ€บ js โ€บ js_json_stringify.asp
JSON.stringify()
Use the JavaScript function JSON.stringify() to convert it into a string.
Discussions

json - Javascript: stringify object (including members of type function) - Stack Overflow
Each Javascript object (with methods) defines the behavior of page components. The page contents (including JS behavior) has to be saved server-side, hence serialized. ... how is the code created? it seems like you should have access to a string representation of all this at some earlier point in the process. ... I hope that this isn't used by the wide public then... otherwise you'll have to make sure that there's no way I can XSS that thing. ... JSON.stringify... More on stackoverflow.com
๐ŸŒ stackoverflow.com
javascript - Convert JS object to JSON string - Stack Overflow
Douglas Crockford, father of the ... for JavaScript. Later Steve Yen at Trim Path wrote a nice improved version which I have used for some time. It's my changes to Steve's version that I'd like to share with you. Basically they stemmed from my wish to make the stringifier: ... You can use JSON.stringify() method to convert JSON object to ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
JSON Stringify and JSON objects with JS functions
I am trying to convert an object in 4D into an object for use in javascript. Specifically i have some code like this. var $oColumn:Object $oColumn:=New Object() $oColumn.columnName:="active" $oColumn.render:="function(data, type, row) {alert('hello world')}" $vtReturn:=JSON Stringify($oColumn) ... More on discuss.4d.com
๐ŸŒ discuss.4d.com
0
0
May 28, 2024
[AskJS] PETITION: Make JSON.stringify use any object's toString method internally!
.toJSON already exists, it's just a not well known feature of JS. 6th bullet point. More on reddit.com
๐ŸŒ r/javascript
16
0
January 3, 2024
๐ŸŒ
Udacity
udacity.com โ€บ blog โ€บ 2021 โ€บ 04 โ€บ javascript-json-stringify.html
Converting Javascript Objects into Strings with JSON.stringify() | Udacity
September 27, 2022 - JSON.stringify() requires one parameter: the Javascript object to be converted into a JSON string. Javascript object literals can be cumbersome to type, so it is often easier and more readable to pass them as a variable.
Top answer
1 of 6
15

You can use JSON.stringify with a replacer like:

JSON.stringify({
   color: 'red',
   doSomething: function (arg) {
        alert('Do someting called with ' + arg);
   }
}, function(key, val) {
        return (typeof val === 'function') ? '' + val : val;
});
2 of 6
9

A quick and dirty way would be like this:

Object.prototype.toJSON = function() {
  var sobj = {}, i;
  for (i in this) 
    if (this.hasOwnProperty(i))
      sobj[i] = typeof this[i] == 'function' ?
        this[i].toString() : this[i];

 return sobj;

};

Obviously this will affect the serialization of every object in your code, and could trip up niave code using unfiltered for in loops. The "proper" way would be to write a recursive function that would add the toJSON function on all the descendent members of any given object, dealing with circular references and such. However, assuming single threaded Javascript (no Web Workers), this method should work and not produce any unintended side effects.

A similar function must be added to Array's prototype to override Object's by returning an array and not an object. Another option would be attaching a single one and let it selectively return an array or an object depending on the objects' own nature but it would probably be slower.

function JSONstringifyWithFuncs(obj) {
  Object.prototype.toJSON = function() {
    var sobj = {}, i;
    for (i in this) 
      if (this.hasOwnProperty(i))
        sobj[i] = typeof this[i] == 'function' ?
          this[i].toString() : this[i];

    return sobj;
  };
  Array.prototype.toJSON = function() {
      var sarr = [], i;
      for (i = 0 ; i < this.length; i++) 
          sarr.push(typeof this[i] == 'function' ? this[i].toString() : this[i]);

      return sarr;
  };

  var str = JSON.stringify(obj);

  delete Object.prototype.toJSON;
  delete Array.prototype.toJSON;

  return str;
}

http://jsbin.com/yerumateno/2/edit

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ javascript-json-stringify-method
JavaScript JSON stringify() Method - GeeksforGeeks
July 11, 2025 - The JSON.stringify() method in JavaScript is used to convert JavaScript objects into a JSON string.
๐ŸŒ
HostingAdvice
hostingadvice.com โ€บ home โ€บ how-to โ€บ javascript "object to string" using json.stringify()
JavaScript "Object to String" Using JSON.stringify()
March 24, 2023 - var obj = {name: "foo", id: 1, age: 45}; JSON.stringify(obj, ['name', 'id']); // outputs: {"name":"foo","id":1}" You could also define a replacer function to get more control over the resulting string output.
Find elsewhere
๐ŸŒ
ServiceNow Community
servicenow.com โ€บ community โ€บ developer-articles โ€บ interesting-facts-about-json-stringify โ€บ ta-p โ€บ 2329985
Interesting facts about JSON.stringify() - ServiceNow Community
June 5, 2021 - If replacer is an array, the array's values indicate the names of the properties in the object that should be included in the resulting JSON string. JSON.stringify(foo, ['week', 'month']); // '{"week":45,"month":7}', only keep "week" and "month" properties ยท The space argument may be used to control spacing in the final string.
๐ŸŒ
ReqBin
reqbin.com โ€บ code โ€บ javascript โ€บ wqoreoyp โ€บ javascript-json-stringify-example
How to stringify a JavaScript object to JSON string?
You can use the JSON.stringify(value, replacer, space) method to stringify a JavaScript object or value to a JSON string. The JSON.stringify() method serializes (converts to a JSON string) objects, arrays, and primitive values.
๐ŸŒ
W3Schools
w3schools.com โ€บ jsref โ€บ jsref_stringify.asp
JavaScript JSON stringify() Method
November 4, 2023 - The JSON.stringify() method converts JavaScript objects into strings. When sending data to a web server the data has to be a string.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ json-stringify-example-how-to-parse-a-json-object-with-javascript
JSON Stringify Example โ€“ How to Parse a JSON Object with JS
March 27, 2022 - Luckily, this works the same way as in the browser โ€“ just use JSON.stringify() to convert JavaScript object literals or arrays into a JSON string:
๐ŸŒ
JavaScript.info
javascript.info โ€บ tutorial โ€บ the javascript language โ€บ data types
JSON methods, toJSON
The idea is to provide as much power for replacer as possible: it has a chance to analyze and replace/skip even the whole object if necessary. The third argument of JSON.stringify(value, replacer, space) is the number of spaces to use for pretty formatting.
๐ŸŒ
ZetCode
zetcode.com โ€บ javascript โ€บ json-stringify
JavaScript JSON.stringify - Converting Objects to JSON
March 7, 2025 - Understand how to use JSON.stringify in JavaScript to convert objects into JSON strings, with examples and explanations.
๐ŸŒ
4D
discuss.4d.com โ€บ english community
JSON Stringify and JSON objects with JS functions - English Community - 4D Forum
May 28, 2024 - Specifically i have some code like this. var $oColumn:Object $oColumn:=New Object() $oColumn.columnName:="active" $oColumn.render:="function(data, type, row) {alert('hello world')}" $vtReturn:=JSON Stringify($oColumn) ...
๐ŸŒ
DhiWise
dhiwise.com โ€บ post โ€บ javascript-essentials-detailed-exploration-of-json-stringify
A Detailed Exploration of JSON Stringify
September 29, 2023 - JSON Stringify is a method in JavaScript that converts JavaScript objects into JSON strings. JSON stands for JavaScript Object Notation, a lightweight data-interchange format that is easy for humans to read and write and easy for machines to ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ how-to-parse-json-object-using-json-stringify-in-javascript
How to parse JSON object using JSON.stringify() in JavaScript ? - GeeksforGeeks
December 6, 2023 - <script> var obj = { name: "Vishal", ... the array is declared inside the object that is being passed as a value to the JSON.stringify() function to be parsed....
๐ŸŒ
Reddit
reddit.com โ€บ r/javascript โ€บ [askjs] petition: make json.stringify use any object's tostring method internally!
r/javascript on Reddit: [AskJS] PETITION: Make JSON.stringify use any object's toString method internally!
January 3, 2024 -

What do you guys think? Should I make an official proposal? What would be the best way to make such spec proposal?

And tell me what you think! The idea is for any object's toString method override the way JSON.stringify creates the string. For example:

var vec =
{
    x: 0,
    y: 0,
    toString() => `[${this.x},${this.y}]`
}

JSON.stringify(vec) //will return: '[0,0]'

This way I won't need to use a custom stream to alter a big object when converting it to JSON!

Or would you say this is very unnecessary, even if an easy thing to add to JS?