jQuery.parseJSON('{"name":"John"}')
converts string representation to object
but I want the reverse. Object is to be converted to JSON string
I got a link http://www.devcurry.com/2010/03/convert-javascript-object-to-json.html
but it need to have json2.js. Does jQuery have a native method to do this?

- 2,056
- 4
- 24
- 47

- 7,889
- 18
- 64
- 95
-
http://stackoverflow.com/questions/18932686/how-to-alert-json-file-data-from-javascript – Jan 03 '14 at 06:21
4 Answers
jQuery does only make some regexp checking before calling the native browser method window.JSON.parse()
. If that is not available, it uses eval()
or more exactly new Function()
to create a Javascript object.
The opposite of JSON.parse()
is JSON.stringify()
which serializes a Javascript object into a string. jQuery does not have functionality of its own for that, you have to use the browser built-in version or json2.js
from http://www.json.org
JSON.stringify()
is available in all major browsers, but to be compatible with older browsers you still need that fallback.

- 4,806
- 5
- 32
- 42

- 231,737
- 57
- 305
- 359
-
4this may be helpful: `var theObject = (typeof data == "string") ? jQuery.parseJSON(data) : data;` – roberthuttinger Mar 05 '12 at 19:52
-
2@tekretic: I hope you're trolling. Chrome implements the `JSON` host object since day 1. – jAndy Sep 05 '12 at 10:18
-
5@jAndy: Oh wow, sorry. Not trolling but OH so wrong. Turns out native JSON support was [added to WebKit in mid 2009](https://bugs.webkit.org/show_bug.cgi?id=20031), making it supported since Chrome 3.0. My previous, totaly inaccurate comment was based on a half of [this outdated question](http://stackoverflow.com/questions/1364842/json-is-not-defined-chrome) combined with the CMS I'm working on at the moment which actually replaces window.JSON with its own library, meaning `JSON.stringify()` isn't defined. Quite a fail on all counts. – Molomby Sep 07 '12 at 02:21
-
13For those interested, here's [a nice chart of native JSON support in different browsers](http://caniuse.com/json). – Molomby Sep 07 '12 at 02:23
-
1@jAndy: dont confuse me; : JSON.stringify 'serialises' an object, please say that its just a mistake – halfbit Jul 29 '15 at 21:14
-
@halfbit Serialization is taking an object and writing it out as a string. Deserialization is taking a string and converting it into an object. – JakeJ Dec 07 '17 at 14:02
Also useful is Object.toSource() for debugging purposes, where you want to show the object and its properties for debugging purposes. This is a generic Javascript (not jQuery) function, however it only works in "modern" browsers.

- 3,258
- 2
- 24
- 32
Convert JavaScript object to json data
$("form").submit(function(event){
event.preventDefault();
var formData = $("form").serializeArray(); // Create array of object
var jsonConvertedData = JSON.stringify(formData); // Convert to json
consol.log(jsonConvertedData);
});
You can validate json data using http://jsonlint.com

- 553
- 7
- 5
You can use the excellent jquery-Json plugin:
http://code.google.com/p/jquery-json/
Makes it easy to convert to and from Json objects.

- 16,359
- 15
- 81
- 92
-
2
-
2SPL = Standard PHP Library. Guess in this case is would be SJL (standard javascript library). – David J Eddy Oct 30 '13 at 15:24
-
3The question is about jQuery & Javascript. It nothing to do with PHP, PHP libraries or SPL. – George Filippakos Oct 31 '13 at 08:50
-
3standard libraries are standard libraries; don't matter the language. My point being it is better to recommend a solution that teaches the language over 'use this plugin' syndrome. – David J Eddy Oct 31 '13 at 15:01
-
sure but why reinvent the wheel? he could always look at the source code of the plugin. – George Filippakos Nov 01 '13 at 16:56
-
1Exactly, why reinvent the wheel...use the standard abilities of a language over a 'plugin' but I digress. – David J Eddy Nov 01 '13 at 18:29
-
How about adding your own answer instead of hijacking everybody else's? If you don't think people should use libraries, or that libraries have no merit then you are in the wrong industry haha. – George Filippakos Nov 02 '13 at 08:30
-