124

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?

Melllvar
  • 2,056
  • 4
  • 24
  • 47
Kuttan Sujith
  • 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 Answers4

191

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.

James Watkins
  • 4,806
  • 5
  • 32
  • 42
jAndy
  • 231,737
  • 57
  • 305
  • 359
  • 4
    this 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
  • 13
    For 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
12

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.

Excalibur
  • 3,258
  • 2
  • 24
  • 32
5

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

Subroto Biswas
  • 553
  • 7
  • 5
2

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.

George Filippakos
  • 16,359
  • 15
  • 81
  • 92