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
๐ŸŒ
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().
๐ŸŒ
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.
๐ŸŒ
ReqBin
reqbin.com โ€บ code โ€บ javascript โ€บ wqoreoyp โ€บ javascript-json-stringify-example
How to stringify a JavaScript object to JSON string?
The JSON.stringify(value, replacer, space) method converts JavaScript objects to a JSON string. The resulting JSON string is a JSON-formatted or serialized object that can be sent over the network or stored on a disk.
๐ŸŒ
W3Schools
localdev.w3schools.com โ€บ js โ€บ js_json_stringify.asp
JSON.stringify()
February 22, 2024 - Use the JavaScript function JSON.stringify() to convert it into a string. ... The result will be a string following the JSON notation.
๐ŸŒ
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.
๐ŸŒ
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. function replacer(key, value) { ...
Find elsewhere
๐ŸŒ
W3Schools
w3schools.io โ€บ file โ€บ json-javascript
Javascript to parse, write, pretty json file(Examples) - w3schools
There are multiple ways we can do it, Using JSON.stringify method ยท JSON stringifymethod Convert the Javascript object to json string by adding the spaces to the JSOn string and printing in an easily readable format.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ javascript-json-stringify-method
JavaScript JSON stringify() Method - GeeksforGeeks
July 11, 2025 - This argument is used to control spacing in the final string generated using the JSON.stringify() function. It can be a number or a string if it is a number then the specified number of spaces are indented to the final string and if it is a ...
๐ŸŒ
Udacity
udacity.com โ€บ blog โ€บ 2021 โ€บ 04 โ€บ javascript-json-stringify.html
Converting Javascript Objects into Strings with JSON.stringify() | Udacity
September 27, 2022 - JSON.stringify() converts Javascript objects into JSON strings. It can manage many types of data type conversion, and you can also add special conversions.
๐ŸŒ
W3Schools
w3schools.com โ€บ js โ€บ tryit.asp
JavaScript JSON
The W3Schools online code editor allows you to edit code and view the result in your browser
๐ŸŒ
W3Schools
w3schools.com โ€บ jsref โ€บ jsref_obj_json.asp
JavaScript JSON Reference
// a JavaScript object...: var myObj = { "name":"John", "age":31, "city":"New York" }; // ...converted into JSON: var myJSON = JSON.stringify(myObj); // send JSON: window.location = "demo_json.php?x=" + myJSON; Try it Yourself ยป ยท For a tutorial about JSON, read our JSON Tutorial.
๐ŸŒ
W3Schools Blog
w3schools.blog โ€บ home โ€บ json.stringify() method
JSON.stringify() method - W3schools
January 26, 2023 - JSON stringify example: Javascript handler JSON.stringify() method is used to transform a JSON object into a JSON string representation.
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

๐ŸŒ
W3Schools
w3schools.com โ€บ js โ€บ tryit.asp
JSON.stringify() will remove any functions from an object.
The W3Schools online code editor allows you to edit code and view the result in your browser
๐ŸŒ
DEV Community
dev.to โ€บ theudemezue โ€บ how-to-stringify-json-data-in-javascript-38d2
How To Stringify JSON Data in JavaScript - DEV Community
March 7, 2025 - I find it really useful to understand how to convert a JavaScript object into a JSON string. This technique is key when saving data to a file, sending information between a browser and a server, or storing configuration settings. In this post, Iโ€™ll share a step-by-step explanation on how to use JSON.stringify, share some code examples, and even cover a few common pitfalls that might trip you up.
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Learn_web_development โ€บ Core โ€บ Scripting โ€บ JSON
Working with JSON - Learn web development | MDN
Here we're creating a JavaScript object, checking what it contains, converting it to a JSON string using stringify() โ€” saving the return value in a new variable โ€” then checking it again.