93
votes

Why does:

JSON.parse('');

produce an error?

Uncaught SyntaxError: Unexpected end of input

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

EDIT: This is not a duplicate of the linked question. While the topic of minimal valid json is related to this question it does not get at the "why".

5
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
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
An empty string is not a valid json, then it fails. Don't think it diserves much more analysis :)Claudio Redi
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
An empty string, as many have mentioned, is not a valid JSON string. Frankly, I think that’s a shortcoming with the specification, as it would have been trivial to return null. Requiring the user to check for empty strings or null, or wrapping JSON.parse inside try … catch should have unnecessary. The fact that the question had to be asked also suggests that it’s not intuitive. In any case, you can use: JSON.parse(data||'{}') to coerce an empty string to something safe.Manngo

5 Answers

157
votes

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'

14
votes

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 :)
}
1
votes

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.

-2
votes

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

-2
votes

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