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!
My name is Fred Hall. My hobby is studying programming languages, which I would like to share with you. Please do not hesitate to contact me if you are having problems learning the computer languages Java, JavaScript, C, C#, Perl, or Python. I will respond to all of your inquiries.
Name of the university: HUSC
Major: IT
Programming Languages: Java, JavaScript, C , C#, Perl, Python