Warning: session_start(): open(/tmp/sess_26d79bc07b494b0a5235c58b945394a5, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Deal with the “Unexpected end of JSON input” Error In JavaScript - LearnShareIT

How To Deal with the “Unexpected end of JSON input” Error In JavaScript

This article will introduce how to fix JavaScript’s “Unexpected end of JSON input” error. This error appears when you try to convert an invalid JSON string to an array using JSON.parse() method and forget to input the value for the method. We tested it effectively by ensuring to input the value first. Please read to the end of the article to know how we can solve this error.

The cause of this error

The error “Unexpected end of JSON input” in JavaScript appears when you try to use the JSON.parse() method to convert Json string to array, but you forgot to assign the input for JSON. This is a common mistake made by new JavaScript programmers. JavaScript will show you an error when you try to do that.

Make sure to parse in a valid JSON variable, meaning the variable doesn’t empty.

Example:

const cars = ["Peugeot", "BMW", "KIA", "Mazda", "Bus", "Truck"];
const jsonToString = JSON.stringify(cars);

// This is the cause of the error. We do not input value for the parse method.
const strToArray = JSON.parse("");
console.log("After convert Json to Array: ", strToArray);

Output

The error will show if you try to run this code.

SyntaxError: Unexpected end of JSON input

To Deal with the “Unexpected end of JSON input” error

The error “Unexpected end of JSON input” will solve by ensuring the variable isn’t empty. It must be valid when we call the JSON.parse() method on it. The other way we can use the try-catch to handle this problem.

Make sure the variable is not null when using the JSON.parse() method.

To deal with the error “Unexpected end of JSON input” in JavaScript. We re-check the code to make sure the variable is valid.

const cars = ["Peugeot", "BMW", "KIA", "Mazda", "Bus", "Truck"];
const jsonToString = JSON.stringify(cars);

// The error will solve when we add the value for the parse() method
const strToArray = JSON.parse(jsonToString);
console.log("After convert Json to Array: ", strToArray);

Output

After convert Json to Array:  [ 'Peugeot', 'BMW', 'KIA', 'Mazda', 'Bus', 'Truck' ]

Using the try-catch() method

The other way is using the try-catch() method to keep out the error. can run other commands in the program

const cars = ['Peugeot', 'BMW', 'KIA', 'Mazda', 'Bus', 'Truck'];
const jsonToString = JSON.stringify(cars);

// This is the cause of the error. We do not input value for the parse method. 
try {
  const strToArray = JSON.parse(''); 
  console.log(strToArray);
} catch (err) {
  // Catch to show the error, and except that
  console.log("error parse string input, please input the value to parse method");
  strToArray = JSON.parse(jsonToString);
}

console.log('After convert Json to Array: ', strToArray); 

Output

Error parse string input, please input the value to parse method
After convert Json to Array:  [ 'Peugeot', 'BMW', 'KIA', 'Mazda', 'Bus', 'Truck' ]

Summary

The “Unexpected end of JSON input” error in JavaScript occurs when you try to use the JSON.parse() method in the not defined value. You must make sure the value used to parse exists. Don’t hesitate to comment below if you have any questions.

Thank you for reading!

Leave a Reply

Your email address will not be published. Required fields are marked *