Unexpected end of JSON input Error in JavaScript

December 8, 2022

Unexpected end of JSON input Error in JavaScript

This guide is part of the “Common JavaScript Errors” series. It’s focused entirely on providing quick and easy solutions for JavaScript-related problems.


The "Unexpected end of JSON input" error is a syntax error that occurs when the JSON you're trying to parse is incomplete or invalid.

This error typically occurs when you're trying to parse JSON using the JSON.parse() method, and the JSON string you're passing to it is either incomplete or invalid. For example, if you're trying to parse the following JSON:

{
  "name": "John Doe"
  "age": 30
}

You would get the "Unexpected end of JSON input" error because the JSON is missing a comma between the "name" and "age" properties.

The correct JSON would be:

{
  "name": "John Doe",
  "age": 30
}

So, one of the things you need to do is to ensure that the JSON you're trying to parse is complete and valid. You can use a JSON linter or a JSON validator to check if your JSON is correct and fix any syntax errors.

When localStorage is full

If the local storage is full or if there is some other problem when trying to save the JSON string, the localStorage.setItem method will also throw the error.

This can also happen if another application already uses the key you're trying to parse, and cannot be overwritten.

function saveUserData(user) {
  const json = JSON.stringify(user);
  localStorage.setItem('user', json);
}

const user = {
  name: 'John Doe',
  email: 'johndoe@gmail.com',
  password: 'p@ssw0rd'
};

saveUserData(user);

In this case, you could add a try/catch block to the saveUserData function and use console.log to print the error message to the console, like this:

function saveUserData(user) {
  try {
    const json = JSON.stringify(user);
    localStorage.setItem('user', json);
  } catch (error) {
    console.log(error.message);
  }
}

With this change, when the saveUserData function is called, it will attempt to save the user object to the local storage. If there is an error, the catch block will be executed and the error message will be printed to the console.


If it helps, here are some examples of syntax errors that will also throw the error.

The JSON input is not properly formatted:

const json = '{ "key": "value"';

The JSON input contains a syntax error:

const json = '{ "key": "value" error }';

The JSON input has a missing or extra comma:

const json = '{ "key": "value", "key2": "value2" "key3": "value3" }';

The JSON input has a missing or extra curly brace:

const json = '{{ "key": "value" "key2": "value2" }';

The JSON input has a missing or extra square bracket:

const json = '{ "key": "value", "key2": ["value2", "value3"]] }';

The JSON input has a missing or extra quotation mark:

const json = '{ key: "value", "key2": "value2" }';

The JSON input has a string that is not properly escaped:

const json = '{ "key": "value", "key2": "value2" "key3": "value3" }';