90

Can someone tell me what the difference is between the 2 JSON parsers?

https://github.com/douglascrockford/JSON-js/blob/master/json.js
https://github.com/douglascrockford/JSON-js/blob/master/json2.js

I have a JSON file from 2007-04-13 (It has methods such as parseJSON). I don't see these methods in any of the new versions.

2
  • 2
    You can find the new file here github.com/douglascrockford/JSON-js May 21, 2011 at 3:43
  • 1
    For anybody who came to this question wondering about what these files are, know that there is no reason to use them in modern browsers. From the GitHub repo: "On current browsers, [json2.js] does nothing, preferring the built-in JSON object. There is no reason to use this file unless fate compels you to support IE8, which is something that no one should ever have to do again." Aug 30, 2017 at 16:12

3 Answers 3

60

From their code:

// Augment the basic prototypes if they have not already been augmented.
// These forms are obsolete. It is recommended that JSON.stringify and
// JSON.parse be used instead.

if (!Object.prototype.toJSONString) {
    Object.prototype.toJSONString = function (filter) {
        return JSON.stringify(this, filter);
    };
    Object.prototype.parseJSON = function (filter) {
        return JSON.parse(this, filter);
    };
}

I guess parseJSON is obsolete, therefore the new version (json2) doesn't even use it anymore. However if your code uses parseJSON a lot you could just add this piece of code somewhere to make it work again:

    Object.prototype.parseJSON = function (filter) {
        return JSON.parse(this, filter);
    };
5
  • 1
    Thanks, so it appears that parseJSON has been replaced by JSON.parse? Also, what about toJSONString? Our existing code uses a lot of these methods: boolean.toJSONString() date.toJSONString() number.toJSONString() object.toJSONString() string.toJSONString()
    – Bob Smith
    Feb 16, 2009 at 3:38
  • 1
    Then also add the 1st piece of code, all the values you specified are Objects, therefore they will all be converted to use JSON.stringify automatically. Feb 16, 2009 at 3:41
  • Thanks! I will give this a try. So, can I add these functions to the json.js file?
    – Bob Smith
    Feb 16, 2009 at 4:33
  • "absolete" - absolute or obsolete?
    – Eric
    Nov 20, 2010 at 20:54
  • 85
    "absolete" - when it's definitely obsolete. Jan 4, 2011 at 17:24
32

Quoting here:

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

26

I also noticed that json2 stringified arrays differently than json2007.

In json2007:

var array = [];
array[1] = "apple";
array[2] = "orange";
alert(array.toJSONString()); // Output: ["apple", "orange"].

In json2:

var array = [];
array[1] = "apple";
array[2] = "orange";
alert(JSON.stringify(array)); // Output: [null, "apple", "orange"].
1
  • 5
    json2 is correct in this case. json2007 was wrong to ignore the first element at index 0.
    – Rob Kinyon
    Jan 31, 2016 at 1:45

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.