18

Possible Duplicate:
Browser-native JSON support (window.JSON)

Specifically, is JSON.parse(...) supported by IE7+, Firefox 2+, Chrome, Safari?

See: JSON in JavaScript

Community
  • 1
  • 1
700 Software
  • 85,281
  • 83
  • 234
  • 341

2 Answers2

48

The answer in 2013 (and later)

Is JSON.parse supported by all major browsers?

Pretty much, yes (source). Even IE8 has it (provided you're not in IE7 emulation mode). If you need to support IE7 and earlier, read on.

The original answer from 2011

No, older browsers (IE7 for instance) mostly don't have it. (More: http://caniuse.com/#search=JSON.parse)

However, just a small script is all you need. The inventor of JSON, Douglas Crockford, has no fewer than three for you to choose from on his Github page:

  • json2.js: Provides both JSON.parse and JSON.stringify. Parsing uses a few regexes to defend against script injection attacks and then passes the result to eval. This isn't generally considered a very good idea.
  • json_parse.js: A recursive-descent parser that doesn't use eval.
  • json_parse_state.js: A state-machine parser that doesn't use eval.

Use what suits you. :-)

Just about any major library (like jQuery, Prototype, YUI, Closure, or any of several others) will also provide JSON parsing, although in some cases it may well be a thin veneer on eval.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 1
    Thank you! This is a very complete answer. As I am using a very dependable server side encoding system, I will probably just say use parse if available and eval if not. And the important thing, as stressed by many people is never use eval unless you know you are getting fully encoded information from your own server and not anyone else's. – 700 Software Feb 05 '11 at 19:05
  • 2
    The JSON object is still not available in IE 8+ if the page is running in IE7 emulated mode using: (a little gotcha to be aware of if writing JS code running in other client's pages.) – reggoodwin Sep 11 '13 at 10:05
  • Update: On the CanIUse page click the "Show All" button to see older browsers that support JSON, which includes +IE8 as noted above. +1 – Yogi Oct 11 '17 at 02:55
1

I am afraid not. You can however use json2 script written by Douglas Crockford.

Here is what John Resig (creator of jQuery) has to say about it:

JSON2.js - Late last year Crockford quietly released a new version of his JSON API that replaced his existing API. The important difference was that it used a single base object (JSON) instead of extending all native object prototypes (booo!).

http://ejohn.org/blog/the-state-of-json/

You can download json2.js here :)

Jeremy Harris
  • 24,318
  • 13
  • 79
  • 133
Sarfraz
  • 377,238
  • 77
  • 533
  • 578