How can I convert a JavaScript object to a JSON string in a JavaScript function? I need the JSON string to pass to a JSP page.
Use the JavaScript function JSON.stringify() to convert it into a string. const myJSON = JSON.stringify(obj); The result will be a string following the JSON notation.
We can have duplicate keys in a JSON object, and it would still be valid. The validity of duplicate keys in JSON is an exception and not a rule, so this becomes a problem when it comes to actual implementations.
The names within an object SHOULD be unique. Save this answer.
JSON with duplicate key entries have to be handled either by ignoring the duplicate entries or by throwing an exception. Until Mule Runtimes 3.8. 6/3.9. 0, JSON Schema Validator handles them by retaining the last duplicate entry and not throwing an exception.
There are two sample methods in Crockford's library (as raised by @Anonymous):
JSON string to object:
var obj = JSON.parse('{ "property":"value" }');
alert (obj.property);
// value
Object to JSON string:
var str = JSON.stringify({ "property":"value" })
alert (str);
//{ "property":"value" }
There are also built in methods to do this in most of the major frameworks.
Quoth Crockford (http://www.json.org/js.html):
To convert a JSON text into an object, you can use the eval() function. eval() invokes the JavaScript compiler. Since JSON is a proper subset of JavaScript, the compiler will correctly parse the text and produce an object structure. The text must be wrapped in parens to avoid tripping on an ambiguity in JavaScript's syntax.
var myObject = eval('(' + myJSONtext + ')');
The eval function is very fast. However, it can compile and execute any JavaScript program, so there can be security issues. The use of eval is indicated when the source is trusted and competent. It is much safer to use a JSON parser. ...
To defend against this, a JSON parser should be used. A JSON parser will recognize only JSON text, rejecting all scripts. In browsers that provide native JSON support, JSON parsers are also much faster than eval. It is expected that native JSON support will be included in the next ECMAScript standard.
var myObject = JSON.parse(myJSONtext, reviver);
And then he develops the JSON prototype in the rest of the article.
The versions of Gecko used in Firefox 3 and 3.5 support JSON natively (https://developer.mozilla.org/En/JSON), which may be useful if your project is limited to a recent Gecko-based application.
As pointed out below, the interesting part about the text generator (not parser) is at https://github.com/douglascrockford/JSON-js/blob/master/json2.js and introduced with
A JSON stringifier goes in the opposite direction, converting JavaScript data structures into JSON text. JSON does not support cyclic data structures, so be careful to not give cyclical structures to the JSON stringifier.
var myJSONText = JSON.stringify(myObject, replacer);
Cyclic data structures and objects that aren't usefully serialized are obviously the only big caveats there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With