1

As we know JSON.parse parses the stringified JSON, but if a variable is already JSON and you try to do JSON.parse on that it throws an error :

> a
[]
> a = JSON.parse(a)
SyntaxError: Unexpected end of input
    at Object.parse (native)
    at repl:1:10
    at REPLServer.defaultEval (repl.js:132:27)
    at bound (domain.js:254:14)
    at REPLServer.runBound [as eval] (domain.js:267:12)
    at REPLServer.<anonymous> (repl.js:279:12)
    at REPLServer.emit (events.js:107:17)
    at REPLServer.Interface._onLine (readline.js:214:10)
    at REPLServer.Interface._line (readline.js:553:8)
    at REPLServer.Interface._ttyWrite (readline.js:830:14)

Why cant JSON.parse verify that argument is already a JSON object and do nothing in that case instead of throwing an error?

2
  • My be owner expected developers to be smart enough to use it.... Commented Apr 19, 2016 at 5:10
  • I think one of the problems will be that [function(){}] will be returned from JSON.parse just like that, although it’s invalid JSON. You’d have to make a check for those cases… By the way, edge cases like JSON.parse({toString: function(){return "[1, 2, 3]";}}) may not return the expected result. Commented Apr 19, 2016 at 5:12

3 Answers 3

5

From the ECMA docs on JSON.parse, it looks like the first thing the parse method does is stringify the first argument using toString(). You can check the docs themselves here: http://www.ecma-international.org/ecma-262/5.1/#sec-15.12.2.

In other words, JSON.parse([]) is equivalent to JSON.parse([].toString()), which is equivalent to JSON.parse(""). Looking at it this way, the error unexpected end of input actually makes much more sense. There's nothing to parse in an empty string.

Answering your question more directly, it would require some performance overhead for JSON.parse to infer and serialize a native javascript object into JSON (JSON isn't javascript). Furthermore, it would violate the single responsibility principle (SRP). It's the responsibility of JSON.parse to parse strings, not objects.

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

Comments

2

Why not use the passed object as a valid JSON
If passed an object to JSON.parse(obj), it is transformed to a string: equivalent to obj.toString() or '' + obj. It does not try to verify the passed object as a valid JSON.

Why an error is generated
The problem with [] empty array is that it transforms to an empty string: '' and this is not a valid JSON.
In case of empty string isn't possible to return a null or undefined(because they have valid JSON strings: JSON.parse('null') or JSON.parse('undefined')) and throwing an error is an useful option.
Check here the exact JSON format.

Equivalent working examples
The following examples are working fine because the object transforms to a valid JSON string:

JSON.parse({toString: function() {return '{"a": 1}'}}); // prints an object {a: 1}
JSON.parse([1]); // prints a number 1
JSON.parse('""') // prints an empty string ""

Comments

1

It's JSON.parse, not JSON.tryParse. If you really need to pass in some input and you don't know if it's stringified or not, you will need to develop your own methods for determining so. I'm glad it doesn't do this. It's current behavior is simple and correctly throws an error if I did something stupid so that I know to correct it.

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.