Unexpected end of JSON input in JavaScript | FIXED


Unexpected end of JSON input in JavaScript | FIXED

This article is about how to fix unexpected end of JSON input in JavaScript.

The unexpected end of JSON input error is usually occurs in JavaScript. it is one of the common error that can be seen when we try to convert a JSON string to a JavaScript value or object using JSON.parse() method.

The JSON.parse() method parse a JSON string and construct a JavaScript object based on the string.

Cause of the error

Here are some common cause of the error:

  1. One if the main cause of this error is parsing a malformed string into JSON.parse() method.
  2. Missing characters or brackets in the JSON sting. So make sure you have properly closed all the curly brackets { } and square brackets [ ].
  3. If you are passing an empty string in the wrong format. We should not pass empty string as JSON.parse(" "), instead do this, JSON.parse({ }).

Solution to fix unexpected end of JSON input Error.

Solution 1 : Make sure you write correct format of JSON:

  • If you want convert string to JSON object, make sure its in JSON format inside ('').
const str = '{"name":"Johnny", "age":30, "city":"California"}'
console.log(JSON.parse(str))

// output: { name: 'John', age: 30, city: 'New York' }
  • If it's an empty string, make sure its
JSON.parse({ })

Solution 2 : If you have a very big JSON file or text, that is hard to find for missing characters, you can use a online JSON validate tool , like jsonlint.com

Hope this two solutions fixes your error 👍.

Related Topics:

Resolve Npm Cannot Find Module Error In Nodejs

How To Fix "Npm ERR Cb() Never Called" | Quick Fix