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 Commented 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." Commented Aug 30, 2017 at 16:12

3 Answers 3

59

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);
    };
Sign up to request clarification or add additional context in comments.

5 Comments

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()
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.
Thanks! I will give this a try. So, can I add these functions to the json.js file?
"absolete" - absolute or obsolete?
"absolete" - when it's definitely obsolete.
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."

Comments

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 Comment

json2 is correct in this case. json2007 was wrong to ignore the first element at index 0.

Your Answer

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