Question Overflow
Question Overflow

Reputation: 11255

jQuery.parseJSON vs JSON.parse

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?

Upvotes: 87

Views: 41944

Answers (7)

Khuram Khan
Khuram Khan

Reputation: 71

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.

Upvotes: 7

giovannipds
giovannipds

Reputation: 3459

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).

Upvotes: 2

Siva Prakash
Siva Prakash

Reputation: 4997

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.

Upvotes: 1

gion_13
gion_13

Reputation: 41533

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.

Upvotes: 3

dfsq
dfsq

Reputation: 193261

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.

Upvotes: 119

leifbennett
leifbennett

Reputation: 69

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..

Upvotes: 6

Joseph
Joseph

Reputation: 119837

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

Upvotes: 11

Related Questions