All current browsers have native JSON support built in. So as long as you're not dealing with prehistoric browsers like IE6/7 you can do it just as easily as that:

var j = {
  "name": "binchen"
};
console.log(JSON.stringify(j));

Answer from Andris on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › JSON › stringify
JSON.stringify() - JavaScript | MDN
If space is anything other than ... the given value, or undefined. ... A BigInt value is encountered. JSON.stringify() converts a value to the JSON notation that the value represents....
🌐
W3Schools
w3schools.com › js › js_json_stringify.asp
JSON.stringify()
JS Examples JS HTML DOM JS HTML Input JS HTML Objects JS HTML Events JS Browser JS Editor JS Exercises JS Quiz JS Website JS Syllabus JS Study Plan JS Interview Prep JS Bootcamp JS Certificate JS Reference ... A common use of JSON is to exchange data to/from a web server. When sending data to a web server, the data has to be a string. You can convert any JavaScript datatype into a string with JSON.stringify().
🌐
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

🌐
HostingAdvice
hostingadvice.com › home › how-to › javascript "object to string" using json.stringify()
JavaScript "Object to String" Using JSON.stringify()
March 24, 2023 - function replacer(key, value) { if (key === "age") { return value + ' years'; } return value; }//end replacer var obj = {name: "foo", id: 1, age: 45}; JSON.stringify(obj, replacer); // outputs: "{"name":"foo","id":1,"age":"45 years"}" Any other manipulation of the key-value pairs is possible with the replacer function control. You’ll often need to indent the stringify output for better readability.
🌐
4D
discuss.4d.com › english community
JSON Stringify and JSON objects with JS functions - English Community - 4D Forum
May 28, 2024 - 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) ...
Find elsewhere
🌐
DigitalOcean
digitalocean.com › community › tutorials › js-json-parse-stringify
How To Use JSON.parse() and JSON.stringify() | DigitalOcean
November 24, 2021 - JSON.stringify() takes a JavaScript object and transforms it into a JSON string.
🌐
Reddit
reddit.com › r/node › using json.stringify on [object object] gives "[object object]" on react
r/node on Reddit: Using JSON.stringify on [object Object] gives "[object Object]" on React
September 16, 2022 -

I am trying to pass an object data from one page to another.

I have a line of code that can pass id and I tried to use it to pass object rather than an integer id but I can't get it right.

The code for passing id from source page:

const navigate = useNavigate();
id && navigate(generatePath("/employeelistedit/:id", { id })); //sample code which works fine when used

The code for passing the object data from source page copying the format of the code above:

function sourcePage(){
const navigate = useNavigate();
var values = {id: "someData", startd: "someData", endd: "someData"}
values && navigate(generatePath("/harvestcalendarmonitoring/:values", { values }));
} //with useNavigate and generatePath

This is the code in another page which receives the data:

const { values } = useParams(); //values gives [object Object]
const x = JSON.stringify(JSON.stringify(values)) //gives "[object Object]"
const y = Object.prototype.toString.call(values) //gives [object String]

For my routing, this is how I wrote it:

<Route path="/harvestcalendarmonitoring/:values" element={< Harvestcalendarmonitoring />} /> //refers to the receiving page

I know I'm not doing it right cause I know that "[object Object]" is showing that something is wrong somewhere in my codes.

Any help and suggestions would be really much appreciated. Thank you in advance.

🌐
JavaScript.info
javascript.info › tutorial › the javascript language › data types
JSON methods, toJSON
The JSON (JavaScript Object Notation) is a general format to represent values and objects. It is described as in RFC 4627 standard. Initially it was made for JavaScript, but many other languages have libraries to handle it as well. So it’s easy to use JSON for data exchange when the client uses JavaScript and the server is written on Ruby/PHP/Java/Whatever. ... JSON.stringify to convert objects into JSON.
🌐
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?

🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-json-stringify-method
JavaScript JSON stringify() Method - GeeksforGeeks
July 11, 2025 - The code demonstrates how to convert a JavaScript object obj into a JSON string using JSON.stringify().
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › JSON › parse
JSON.parse() - JavaScript | MDN
In order for a value to properly round-trip (that is, it gets deserialized to the same original object), the serialization process must preserve the type information. For example, you can use the replacer parameter of JSON.stringify() for this purpose:
🌐
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 - We all know the primary purpose for JSON.stringify(), The JSON.stringify() method converts a JavaScript object or value to a JSON string. However there are 2 interesting parameter that can be used to simplify developer's job. One can be used to filter the JSON and other can be used to indent it.
🌐
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
January 5, 2021 - 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:
🌐
W3Schools
w3schools.com › jsref › jsref_stringify.asp
JavaScript JSON stringify() Method
The JSON.stringify() method converts JavaScript objects into strings. When sending data to a web server the data has to be a string.
🌐
ZetCode
zetcode.com › javascript › json-stringify
JavaScript JSON.stringify - Converting Objects to JSON
Understand how to use JSON.stringify in JavaScript to convert objects into JSON strings, with examples and explanations.