How can I serialize an object to JSON in JavaScript?
Asked
Active
Viewed 2.6e+01k times
4 Answers
298
You’re looking for JSON.stringify
.
Examples:
const object = {
hello: "world",
"some array": [ 42, 69, 420, 1337, null, true ]
},
primitive = false;
console.log(JSON.stringify(object)); // "{\"hello\":\"world\",\"some array\":[42,69,420,1337,null,true]}"
console.log(JSON.stringify(primitive)); // "false"

Sebastian Simon
- 18,263
- 7
- 55
- 75

Mike_G
- 16,237
- 14
- 70
- 101
-
1Using JSON.stringify() with javascript objects containing arrays have a little thing to it. Please check my question: http://stackoverflow.com/questions/25423883/parsing-json-array-gives-error – uylmz Aug 21 '14 at 12:33
-
@uylmz I don’t think this bug exists anymore in 2023. – Sebastian Simon Jan 15 '23 at 12:44
52
Download https://github.com/douglascrockford/JSON-js/blob/master/json2.js
, include it and do
var json_data = JSON.stringify(obj);

bartektartanus
- 15,284
- 6
- 74
- 102

Johannes Weiss
- 52,533
- 16
- 102
- 136
-
1Should I really need to include "json2.js"? It seems to works without it. – Pavel Alexeev Mar 31 '12 at 08:28
-
33@PavelAlexeev No, you don't neet to include `json2.js` anymore, unless you are targetting very old browsers: modern browsers include a native implementation of the `JSON` object. The good thing about `json2.js` is that it will only kick in if no native object is found. See [http://stackoverflow.com/questions/891299/browser-native-json-support-window-json] for a detailed breakdown of browser support. – Edurne Pascual May 10 '12 at 08:14
4
Just to keep it backward compatible I load Crockfords JSON-library from cloudflare CDN if no native JSON support is given (for simplicity using jQuery):
function winHasJSON(){
json_data = JSON.stringify(obj);
// ... (do stuff with json_data)
}
if(Object.prototype.hasOwnProperty.call(window, "JSON") && typeof JSON.stringify === 'function'){
winHasJSON();
} else {
$.getScript('//cdnjs.cloudflare.com/ajax/libs/json2/20121008/json2.min.js', winHasJSON)
}

AvL
- 3,083
- 1
- 28
- 39
-
1`typeof JSON === "object"` is not a safe check. `if(typeof JSON === 'object' && typeof JSON.stringify === 'function')` will fail if, for whatever reason, `JSON === null`. You’re looking for an existence check. The correct way is `if(Object.prototype.hasOwnProperty.call(window, "JSON") && typeof JSON.stringify === "function")` (in modern browsers, it would be `Object.hasOwn(globalThis, "JSON")`, but, of course, modern browsers have `JSON`). According to [Edurne Pascual’s comment](/questions/558518/serializing-an-object-to-json/558534#comment13620495_558536), this check is actually redundant. – Sebastian Simon Jan 15 '23 at 12:40
0
How to work with JSON in Javascript
Having to work with JSON is a common situation in web development, this is why Javascript provides the JSON
object with its static methods.
Parsing from string to object
To parse a JSON string into a Javascript object we can use JSON.parse()
let obj = JSON.parse('{"x": 325, "y": 896, "speed": 16.5}')
Result:
obj = {
x: 325,
y: 896,
speed: 16.5
}
Parsing from object to string
Converting a Javascript object to a string it's as easy as the inverse operation
let str = JSON.stringify({x: 325, y: 896, speed: 16.5})
Result:
str = '{"x": 325, "y": 896, "speed": 16.5}'

pasta64
- 302
- 2
- 11