Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

Why does:

JSON.parse('');

produce an error?

Uncaught SyntaxError: Unexpected end of input

Wouldn't it be more logical if it just returned null?

share|improve this question
    
yeah it seems like a bug in the design of the parse function. you can wrap it in try catch... but that's ugly :) – vsync Jun 3 '15 at 13:40
3  
It produces an error when anything that isn't valid JSON is passed in, like an empty string. It's actually not that uncommon to create an alias that is wrapped in a try/catch to avoid such errors. – adeneo Jun 3 '15 at 13:42
    
An empty string is not a valid json, then it fails. Don't think it diserves much more analysis :) – Claudio Redi Jun 3 '15 at 13:42
    
Please read JSON defenition here en.wikipedia.org/wiki/JSON You can check JSON in any JSON Validator, f.e jsonlint.com if you need empty JSON you have to use {} – Hayk Mantashyan Jun 3 '15 at 13:43
    
up vote 32 down vote accepted

As an empty string is not valid JSON it would be incorrect for JSON.parse('') to return null because "null" is valid JSON. e.g.

JSON.parse("null");

returns null. It would be a mistake for invalid JSON to also be parsed to null.

While an empty string is not valid JSON two quotes is valid JSON. This is an important distinction.

Which is to say a string that contains two quotes is not the same thing as an empty string.

JSON.parse('""');

will parse correctly, (returning an empty string). But

JSON.parse('');

will not.

Valid minimal JSON strings are

The empty object '{}'

The empty array '[]'

The string that is empty '""'

A number e.g. '123.4'

The boolean value true 'true'

The boolean value false 'false'

The null value 'null'

share|improve this answer
    
OK, that makes sense, thanks! – Richard Jun 3 '15 at 14:52
    
JSON.parse('""') doesn't work I got the error: JSON::ParserError: 757: unexpected token at '""' – Sam Dec 8 '15 at 1:28
    
I just ran JSON.parse('""'); in the Chrome console and it works as expected. – bhspencer Dec 8 '15 at 2:11
    
Yet a bare null value is not valid JSON either and JSON.parse(null) === null. JS craziness! – abhillman Dec 3 '16 at 23:37
    
@abhillman that is because all valid JSON must be a string and null is not a string. – bhspencer Dec 4 '16 at 19:45

Use try-catch to avoid it:

var result = null;
try {
  // if jQuery
  result = $.parseJSON(JSONstring);
  // if plain js
  result = JSON.parse(JSONstring);
}
catch(e) {
  // forget about it :)
}
share|improve this answer

JSON.parse expects valid notation inside a string, whether that be object {}, array [], string "" or number types (int, float, doubles).

If there is potential for what is parsing to be an empty string then the developer should check for it.

If it was built into the function it would add extra cycles, since built in functions are expected to be extremely performant, it makes sense to not program them for the race case.

share|improve this answer
1  
This is not correct. Try parsing a string that contains two quotes JSON.parse('""'); – bhspencer Jun 3 '15 at 13:51
2  
Json is not a markup language, it would be more accurate to say JSON.parse expects a string that conforms to the specification. – bhspencer Jun 3 '15 at 14:02
1  
JSON doesn't support the int primitive type. Its called "number" and supports real values not just ints. – bhspencer Jun 3 '15 at 14:11

Because '' is not a valid Javascript/JSON object. An empty object would be '{}'

For reference: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse

share|improve this answer

For a valid JSON string at least a "{}" is required. See more at the http://json.org/

share|improve this answer
    
This is not correct. A string that contains just two quote is valid JSON. – bhspencer Jun 3 '15 at 13:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.