Unexpected end of json input что это

от admin

About "SyntaxError: Unexpected end of JSON input" in JavaScript

Update: This post was originally published on my blog decodingweb.dev, where you can read the latest version for a �� user experience.

You might have encountered the error “Uncaught SyntaxError: Unexpected end of JSON input” when using JSON.parse() .

The error looks like this on the browser console (Google Chrome):

First of all, it’s not your code! What’s most likely wrong is the JSON string you’re trying to parse.

When “Unexpected end of JSON input” is raised, your code is probably parsing:

  1. An empty string
  2. An empty array
  3. Or an incomplete (a.k.a malformed) JSON data

That said, the first thing to examine is the JSON data.

If you’re using Google Chrome (like me), you can click the link on the right-hand side of the error message.

It’s usually in the form of VM: Script ID , e.g., VM:1

If the log is empty, you’re probably dealing with an empty string! Otherwise, you’ll see the JSON data with an indicator.

How to fix the Uncaught SyntaxError: Unexpected end of JSON input

When working with JSON-formatted data in JavaScript, we’re either working with locally defined JSON data or fetching it from an API.

But how can we resolve the issue?

When working with locally defined JSON data: If you’re parsing local JSON content, the first thing to do is to ensure the variable isn’t empty. Maybe the variable is truncated for some reason.

If it’s not, you can validate your JSON-formatted data with an online JSON validator like JSONLint. If you have an invalid JSON object, you’ll find out here!

When working with a third-party JSON API: If you’re trying to parse a JSON API response, and you don’t have control over the back-end code, you can place your JSON.parse reference into a try/catch block – like so:

Exit fullscreen mode

Wrapping up

The infamous «JSON input error» is a Syntax error usually raised when parsing empty or malformed JSON data. Fixing this issue is easy when dealing with locally-defined JSON data.

If you don’t have control over the back-end code generating the JSON data, a try/catch block would help you avoid the «Uncaught SyntaxError» error.

SOLVED — Unexpected end of JSON input WordPress Error

How to fix the “Updating failed. Error message: Unexpected end of JSON input” error in WordPress when trying to update a post using the Perfmatters plugin? I just found a fix!

My first step was to try disabling WordPress plugins to see where origin of the error was. I found the Perfmatters plugin was causing the error. Given this plugin is essential for running my WooCommerce shop at https://jerrybanfield.com/shop/ I knew I had to play with the plugin settings rather than disable it. I found a fix easily by testing different settings.

Simple TURN OFF disable Google Fonts and the error goes away! Easy money!

How To Fix The Unexpected End of Input Error in JS

Code formatting. Tabs or spaces, semi-colons or no semi-colons. It is a pretty controversial subject to many but it is quite important in some instances. If you are on a team, having a cohesive code format helps code readability among your peers. Even if you work alone, one big benefit of having a good sense of code formatting is to avoid syntactical errors.

JavaScript is pretty open when it comes to code format. There is a wide range of different ways to format your codebase in this language. What can happen if you don’t do it? Well, an example of a simple error that is often caused by code formatting issues is the Unexpected end of input error. How does it work?

The Problem

When JavaScript code is run it uses just in time compilation (JIT) to turn your code into something the computer can do. When this happens your code is read and certain things are expected about the code, for example having matching parentheses. If you received the Unexpected end of input error, odds are you are missing a closing curly brace > so the error is basically saying “you opened the curly brace but the code ended before it was closed”.

Here’s an example:

As you can see, the code is clearly missing a ending curly brace at the end of the arrow function which causes the error. So how does the code formatting mentioned earlier fit into this? Let’s look at a more real-world example:

In this example, it is a little less clear where the error is. The indentation is not very consistent so you might not notice that the if statement is actually missing a curly brace after which causes the error.

The Solution

Fortunately this is pretty simple to fix — you can just add your missing curly brace. In the above example:

It can definitely be challenging to find a missing curly brace. Depending on your code editor of choice, you may be able to configure different colors for each pair of curly brace so it is easier to see which ones match and which ones don’t.

Another approach is to try and avoid these errors from the start. Using formatting tools such as Prettier or linting tools like ESLint can help a lot, at least in my experience.

Unexpected end of JSON input

There’s a chance that you received a similarly named but slightly different error: Unexpected end of JSON input . Rather than simply a missing curly brace, this error often occurs when you are trying to parse a JSON string that is itself missing a curly brace. Here’s an example:

This can be simply fixed by ensuring the string you are parsing is valid JSON if you have control over that string. If you don’t, you can wrap the whole thing in a try / catch to be safe. Using the previous example:

If you add this to the parseJson function, you can now handle invalid JSON strings rather than getting an unexpected runtime error.

Conclusion

Hopefully this helps you see how useful good code formatting can be in your code, whether you work with a team or alone. Let us know if you have any other reasons why formatting can be so useful or how you do it in your own code setup. Thanks for reading!

codex-source-iconJavaScript error (Uncaught SyntaxError: Unexpected end of input)

Golang json Unmarshal "unexpected end of JSON input"

I am working on some code to parse the JSON data from an HTTP response. The code I have looks something like this:

The json in the respBytes variable looks like this:

However, err is not nil. When I print it out it says unexpected end of JSON input . What is causing this? The JSON seems to valid. Does this error have something to do with my custom struct?

Thanks in advance!

Stratus3D's user avatar

4 Answers 4

The unexpected end of JSON input is the result of a syntax error in the JSON input (likely a missing » , > , or ] ). The error does not depend on the type of the value that you are decoding to.

I ran the code with the example JSON input on the playground. It runs without error.

The code does not decode anything because the result field is not exported. If you export the result field:

then the input is decoded as shown in this playground example.

I suspect that you are not reading the entire response body in your application. I suggest decoding the JSON input using:

Читать:
Как сохранить образ контейнера в файл

Похожие статьи