Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the JSON global object?

I'm reading that the best way to parse JSON in the browser is by using the JSON.parse() method.

Sorry, I've been living under a rock — where the hell did this JSON global object come from? Is it defined in some standard? Is it available in all browsers? When should I use Crockford's json2.js instead?

like image 561
a paid nerd Avatar asked Feb 13 '26 11:02

a paid nerd


1 Answers

It is part of ECMAScript 5, and is the Object with the internal class JSON that holds relevant methods (stringify and parse) for processing JSON data.

Use the json2 library in browsers where JSON is not implemented.

You could test for it like this:

if( Object.prototype.toString.call( window.JSON ) !== '[object JSON]' ) {
    // load the library
}
like image 97
user113716 Avatar answered Feb 15 '26 02:02

user113716