jQuery.parseJSON vs JSON.parse

JavascriptJquery

Javascript Problem Overview


jQuery.parseJSON and JSON.parse are two functions that perform the same task. If the jQuery library is already loaded, would using jQuery.parseJSON be better than using JSON.parse, in terms of performance?

If yes, why? If no, why not?

Javascript Solutions


Solution 1 - Javascript

Here is an extract from jQuery 1.9.1:

parseJSON: function( data ) {
	// Attempt to parse using the native JSON parser first
	if ( window.JSON && window.JSON.parse ) {
		return window.JSON.parse( data );
	}

	if ( data === null ) {
		return data;
	}

	if ( typeof data === "string" ) {

		// Make sure leading/trailing whitespace is removed (IE can't handle it)
		data = jQuery.trim( data );

		if ( data ) {
			// Make sure the incoming data is actual JSON
			// Logic borrowed from http://json.org/json2.js
			if ( rvalidchars.test( data.replace( rvalidescape, "@" )
				.replace( rvalidtokens, "]" )
				.replace( rvalidbraces, "")) ) {

				return ( new Function( "return " + data ) )();
			}
		}
	}

	jQuery.error( "Invalid JSON: " + data );
},

As you can see, jQuery will use the native JSON.parse method if it is available, and otherwise it will try to evaluate the data with new Function, which is kind of like eval.

So yes, you should definitely use jQuery.parseJSON.

Solution 2 - Javascript

According to jQuery

>Where the browser provides a native implementation of JSON.parse, jQuery uses it to parse the string.

thus it means that jQuery provides a JSON parser if no native implementation exists on the browser. here's a comparison chart of browsers that have (and don't have) JSON functionality

Solution 3 - Javascript

If you're using jQuery version 3 (released in 2016) then you should use JSON.parse() because jQuery.parseJSON() has been deprecated.

> As of jQuery 3.0, $.parseJSON is deprecated. To parse JSON objects, use the native JSON.parse method instead.

Solution 4 - Javascript

JSON.parse() is natively available on some browsers, not on others, so it's safer to use a library. The JQuery implementation works well, as other respondents have noted. There's also Douglas Crockford's JSON library, which uses the native implementation if available.

The JSON library has the advantage that it has a method to turn a JavaScript object into a JSON string, which is missing from jQuery at the moment..

Solution 5 - Javascript

I don't know about performance, but it's definitely safer to use the jQuery method because some browsers like ie7 and lower might not have any JSON functionalities natively.
It's all about compatibility, just like you use jQuery's each method instead of the array's native forEach method for iteration.

Solution 6 - Javascript

Talking about performance, the most updated answer is JSON.parse.

The native JSON object is supported in every browser nowadays, so opt for JSON.parse. You can see the support table here: http://caniuse.com/#feat=json

You can also search for this alias appearances at JQuery's repository on GitHub: https://github.com/jquery/jquery/search?utf8=%E2%9C%93&q=parseJSON

Also, jQuery.parseJson was deprecated on version 3.0+ as mentioned by other answers here.

You should only use jQuery's version if you're an old JQuery version + if you want to provide support for very old browsers (normally, not recommended).

Solution 7 - Javascript

jQuery internally uses JSON.parse to parse the JSON file.So it doesn't make any difference in most cases.

But some of the older browsers don't support JSON.parse functionality.In that case using jQuery.parseJSON is beneficial as jQuery can handle JSON using its own function.

NOTE:

> jQuery.parseJSON is deprecated from jQuery 3.0.So please use the native JSON.parse method.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionQuestion OverflowView Question on Stackoverflow
Solution 1 - JavascriptdfsqView Answer on Stackoverflow
Solution 2 - JavascriptJosephView Answer on Stackoverflow
Solution 3 - JavascriptKhuram KhanView Answer on Stackoverflow
Solution 4 - JavascriptleifbennettView Answer on Stackoverflow
Solution 5 - Javascriptgion_13View Answer on Stackoverflow
Solution 6 - JavascriptgiovannipdsView Answer on Stackoverflow
Solution 7 - JavascriptSiva PrakashView Answer on Stackoverflow