86

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?

Rory O'Kane
  • 29,210
  • 11
  • 96
  • 131
Question Overflow
  • 10,925
  • 18
  • 72
  • 110
  • 1
    I think JSON.parse isn't avaible at old browsers. In terms of speed they should be identical, JSON.parse should be little more faster (I think jQuery uses JSON.parse in newer browsers). –  Apr 28 '12 at 09:33

7 Answers7

118

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.

Rory O'Kane
  • 29,210
  • 11
  • 96
  • 131
dfsq
  • 191,768
  • 25
  • 236
  • 258
  • 3
    Wow, great answers! Thanks everyone, I think yours is the most comprehensive answer. – Question Overflow Apr 28 '12 at 09:39
  • Depending on where your json comes from, there may be security issues with the fallback 'eval' method. – Steve Mayne Apr 28 '12 at 09:41
  • The rvalidchars.test stuff should catch invalid JSON; so this looks pretty safe. – Daniel Nov 28 '12 at 17:52
  • 6
    Note that this is old, and an empty string will no longer return null. It will instead throw an error. – mlissner Apr 29 '13 at 18:51
  • 3
    Yes, I think this answer should be updated to state that it's almost unnecessary to use a big library like jQuery for this. Gonna report Joseph the Dreamer's link to show the browser adoption of `JSON.parse`: http://caniuse.com/#search=json – Hulvej May 18 '16 at 14:15
12

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

Joseph
  • 117,725
  • 30
  • 181
  • 234
7

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.

Bob Stein
  • 16,271
  • 10
  • 88
  • 101
Khuram Khan
  • 71
  • 1
  • 1
6

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

leifbennett
  • 69
  • 1
  • 1
3

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.

gion_13
  • 41,171
  • 10
  • 96
  • 108
  • 1
    I always use JSON.parse(date) because of I never support IE8- and less characters than $.parseJSON() ^_^ For IE8- let's use like this – xicooc Jan 16 '15 at 03:35
2

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

giovannipds
  • 2,860
  • 2
  • 31
  • 39
1

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.

Siva Prakash
  • 4,626
  • 34
  • 26