“Unexpected end of JSON input” – possible fixes

"Unexpected end of JSON input" (or "Uncaught SyntaxError: Unexpected end of JSON input") is a common error message in JavaScript. Developers often encounter it when they try to convert a string into a native JS object using JSON.parse() method. This short article will try to clarify a few things about the error and possible steps to fix it.

"Unexpected end of JSON input" causes

The full form of the message would look like this in the browser's Console.

Uncaught SyntaxError: Unexpected end of JSON input
at JSON.parse (<anonymous>)
at <your-script-name.js>

Unexpected end of JSON input in Chrome console

"Unexpected end of JSON input" root cause is a malformed string passed into the JSON.parse() method.

In most cases, it is due to a missing character in the last position of a JSON string that wraps its objects (such as a closing square bracket [] or curly bracket {}).

Sometimes, you may be trying to read an empty JSON file. A valid empty JSON file would still need to have curly brackets to indicate that it contains an empty object.

// empty.json file contents
{}

Let's look at two examples below to clearly understand what's missing

Code containing errors Corrected code.
1 var my_json_string = '{"prop1": 5, "prop2": "New York"'; var data = JSON.parse(my_json_string); var my_json_string = '{"prop1": 5, "prop2": "New York"}'; var data = JSON.parse(my_json_string);
2 var my_json_string = '[1, "yellow", 256, "black"'; var data = JSON.parse(my_json_string); var my_json_string = '[1, "yellow", 256, "black"]'; var data = JSON.parse(my_json_string);

In the first example, there's a missing closing curly bracket at the end of the string. Meanwhile, the second example demonstrate a malformed JSON string with the closing square bracket truncated.

How to fix "Unexpected end of JSON input"

  1. Locate a statement using the JSON.parse() method. On the browser's console, click on the last line of the exception message (which is a reference to the code block that raised that exception). The browser will then bring you to the actual source code.

  2. Inspect the input of JSON.parse(). Now there are many ways to do this. You can take a close look at the data to spot the error. Usually it's in the beginning or the end of the string.

  3. If you use popular code editor software like VS Code, Sublime Text, Atom, you'll also have another way to check the syntax of JSON data: Copy all that JSON data to a completely new file, the default formatter of the software will highlight the syntax error location.

  4. Alternatively, the browser Console also supports highlighting common JSON syntax error. You would need to click VMxx:x right next to the exception message.

    image-20211020085539363

Conclusion

We hope that the article helps you understand why "Unexpected end of JSON input" happens and how you can correct the input to fix it. If you do a lot of JSON manipulation in JavaScript, you may want to check out our guide on JSON end of file expected, which is another very common one. If you have any suggestions or spot an error in the article, feel free to leave a comment below to let us know.

Click to rate this post!
[Total: 1 Average: 5]

Leave a Comment