JSON’s end of file expected – everything you need to know

What “EOF” means

In layman’s terms, EOF (end-of-file) is simply an indicator for operating system so it knows where to stop reading a data source properly. A data source could be a file or a stream.

Without EOF, the system may either keeps reading and waiting for new data or stop immediately.

JSON reader should raise an error message to show you that the actual JSON file is malformed. The error message may look like this :

Expecting 'EOF', '}', ',', ']', got '{'Code language: JavaScript (javascript)

Or nicely displayed in a few IDEs.

JSON end of file expected in VScode

Also see: Object of type is not JSON serializable in Python

Manually inspect JSON files for syntax errors

If you get “Expecting EOF” or “End of file expected”, it’s likely that the actual JSON file have syntax errors.

JSON is easy, but it does have a few rules. Most of the time, your JSON file structure violates one of these :

  • Array elements should be separated by a comma.
  • Everything must be wrapped in one single root object, which can be an Object {} or an Array {}. Multiple root object is not allowed.
  • Special characters must be escaped. Pay attention closely to double quotes and Unicode double quotes. Here’s a list of special, reserved characters in JSON :
    • Backspace should be escaped with b.
    • Form feed should be escaped with f.
    • Newline should be escaped with n.
    • Carriage return should be escaped with r.
    • Tab should be escaped with t.
    • Double quote should be escaped with “
    • Backslash should be escaped with

Verify errors against the checklist above. After some time, you would get the hang of it and spot the error quicker.

JSON end of file expected with Visual Studio Code on MacOS

Mac OS is equipped with an additional keyboard layout called “ABC extended”. This layout allows you to type special but popular latin characters such as accented carons (č), ogoneks (ą), dots (), thorns (þ) and others.

Special characters are typed with a combination of keystrokes, so a few of them may interfere with the program’s hotkey shortcuts.

In this case, MacOS is the operating system, so it takes precedence and override Visual Studio Code settings.

For example, Option + Shift + F always returns “~” character instead of running “Format Document” in Visual Studio Code.

If you don’t really need support for special characters, the temporary workaround is switching back to ABC layout. If you don’t know how to do that, follow the steps below :

  1. Go to the Apple menu and open Systems Preferences.
  2. Click the Languages and Regions icon on the first row of the Systems Preferences panel.
  3. lick the Keyboard Preferences button at the bottom of the window to open the keyboard preferences.
  4. Click the Input Sources tab.
  5. Click the + button to see a list of languages with keyboards. The U.S. Extended keyboard is listed under English. Click Add to ensure that they keyboard is activated.

Automatically fix poorly formatted JSON files

Sometimes, your JSON files are too big so that manually inspecting it would make no sense.

There have been people faced with such issues, so they developed automated tools to fix invalid JSONs without a human eye looking closely into it. Combine with a diff tool, you can clearly see what part of the JSON file is invalid so that next time, you know where to look at .

This is a non-exhausted list of the JSON format fixer I’ve personally used :

Leave a Comment