How to fix unexpected end of json input – How to Fix Unexpected end of JSON input Error in JavaScript?

How to fix unexpected end of json input: When attempting to parse invalid JSON using the JSON.parse or $.parseJSON functions, the “Unexpected end of JSON input” error occurs.

The issue arises while attempting to parse a value such as an empty array or string. To fix the issue, make sure the JSON is valid before parsing it.

Passing an Empty Array or String to the JSON.parse

index.html:

// Pass an empty array to the JSON.parse() function
// and print the result
console.log(JSON.parse([]));

// Pass an empty string to the JSON.parse() function
// and print the result
console.log(JSON.parse(''));

// Here in both the cases SyntaxError: Unexpected end of JSON
// input occurs

Output:

undefined:1


SyntaxError: Unexpected end of JSON input
at JSON.parse (<anonymous>)
at Object.<anonymous> (/tmp/ER7rLDlJor.js:3:18)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)

Explanation:

Here we got the error after passing an empty array and an empty string  
to the JSON.parse function. 
The same thing would happen if you used the $.parseJSON function.

Passing an Empty Array or String to the $.parseJSON() Function

// Pass an empty string to the $.parseJSON() function
// and print the result
// Here we get an error
console.log($.parseJSON(''));

Explanation:

The error occurs because we are attempting to parse invalid JSON.

The error also arises if you attempt to parse an empty server response or if your server fails to send the relevant CORS headers with the response.

If your server returns an empty response and you attempt to parse it, you will receive an error. You should delete the parsing logic in this situation.

If you’re getting the value from your server, ensure sure the server is setting Content-Type header to application/json.

Fixing Unexpected end of JSON input Error in JavaScript

Json parse unexpected end of input: The “Unexpected end of JSON input” error can be resolved in three ways:

  • In a try/catch block, wrap your parsing logic.
  • Ensure that your server returns a valid JSON response
  • If you expect an empty server response, remove the parsing logic from your code.

NOTE:

You may inspect your server's response by opening your developer tools
 and selecting the Network tab. 
The server's response can be viewed by clicking the Response tab.

Using try/catch Blocks to Avoid Error

index.html:

// Use try-catch blocks to avoid "Unexpected end of JSON input"
// error
try {
  // Pass an empty string to the JSON.parse() function
 // and store it in a variable
  const rslt = JSON.parse('');
  // Print the above result 
  console.log(rslt);
} 
// Handle the "Unexpected end of JSON input" Error inside the 
// catch block
catch (error) {
  // If the error occurs the print some random text and
  // corresponding error
  console.log('Sorry There is an Error!!!!\n', error);
}

Output:

Sorry There is an Error!!!!
SyntaxError: Unexpected end of JSON input
at JSON.parse (<anonymous>)
at Object.<anonymous> (/tmp/ER7rLDlJor.js:6:21)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)

If the JSON.parse() function returns an error as a result of parsing invalid JSON, the error is given as a parameter to the catch function, where it can be handled.

If you know the server’s response does not include valid JSON, you can delete/remove the call to the JSON.parse function.

You can also use an online JSON validator to determine whether a JSON string is valid.