125

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?

1

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

6 Comments

this may be helpful: var theObject = (typeof data == "string") ? jQuery.parseJSON(data) : data;
@tekretic: I hope you're trolling. Chrome implements the JSON host object since day 1.
@jAndy: Oh wow, sorry. Not trolling but OH so wrong. Turns out native JSON support was added to WebKit in mid 2009, making it supported since Chrome 3.0. My previous, totaly inaccurate comment was based on a half of this outdated question 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.
@jAndy: dont confuse me; : JSON.stringify 'serialises' an object, please say that its just a mistake
|
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.

Comments

6

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

Comments

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.

8 Comments

Please recommend SPL methods over add-on logic libraries.
SPL = Standard PHP Library. Guess in this case is would be SJL (standard javascript library).
The question is about jQuery & Javascript. It nothing to do with PHP, PHP libraries or SPL.
standard 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.
Exactly, why reinvent the wheel...use the standard abilities of a language over a 'plugin' but I digress.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.