1

I was looking for the method to do jQuery.parseJSON in JavaScript that parses a json to return a JavaScript object. I can't use jQuery since the whole plugin that I have built is standalone JS and no jQuery has been used till now. Is there something of this sort that's already provided in JavaScript?

2
  • @KevinB : Editted the question. I intended to mention jQuery.parseJSON Commented Feb 27, 2013 at 18:51
  • My comment still applies. Commented Feb 27, 2013 at 18:52

3 Answers 3

1

Use the native JSON object (this is the only time it is correct to say "JSON object", it is literally an object named JSON) to manipulate JSON strings.

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/JSON

Use JSON.parse(yourJSONString); to serialize and JSON.stringify(yourJSONObject); to deserialize.

If you look at the jQuery core source on line 492, jQuery.parseJSON is just an alias to JSON.parse.

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

1 Comment

jbabey Editted the question. I intended to mention jQuery.parseJSON
0

You can either use the native JSON object, which is supported in most browsers, but you will run into an issue when you try to use it in the dinosaur browsers such as IE7 and lower. There is the option to include a standalone plugin that mimics native functionality here (JSON.js).

Comments

0

Short answer:

Use the browser native method JSON.parse()

window.JSON.parse(jsonString);

Long answer:

To get it working in old browsers, you can take the source code of jQuery.parseJSON and remove any dependencies on jQuery itself. Here is a working standalone version:

function standaloneParseJson ( 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;
    }

    var rvalidchars = /^[\],:{}\s]*$/;
    var rvalidbraces = /(?:^|:|,)(?:\s*\[)+/g;
    var rvalidescape = /\\(?:["\\\/bfnrt]|u[\da-fA-F]{4})/g;
    var rvalidtokens = /"[^"\\\r\n]*"|true|false|null|-?(?:\d+\.|)\d+(?:[eE][+-]?\d+|)/g;

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

        // Make sure leading/trailing whitespace is removed (IE can't handle it)
        data = data.replace(/^\s+|\s+$/g, '');

        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 ) )();
            }
        }
    }

    // Error code here
    //jQuery.error( "Invalid JSON: " + data );
}

Comments

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.