Fix the JSON “End of File Expected” Error

What the JSON “End of file expected” error means, the usual causes such as multiple top-level values or trailing text, how to tell them apart, and safe ways to fix it.

An “End of file expected” message means the parser read one complete JSON value and then found more content where the document should have ended. A JSON document holds exactly one top-level value, such as an object, an array, a string, a number, true, false or null. Only whitespace may follow it. This guide explains what triggers the error, how to work out which cause applies, and when a formatter or auto-repair tool helps or gets in the way.

Short answer: is a formatter or validator the right tool here?

A validator that reports a line and column is the most useful first step, because it shows where the first value ended and where the extra content begins. A pretty-printer is helpful after the structure is valid, but it cannot fix this error and will usually refuse the input. Auto-repair tools can make the file parse. The risk is that they guess your intent: they might wrap values in an array, delete trailing text or merge objects, and any of those can change the data. Plain text in a basic editor is the better choice when the file holds credentials or other sensitive content you do not want to paste into a web tool. It is also the better choice when the extra content is meaningful, such as a second record, and you need to decide yourself how it should be combined.

What the error actually means

The JSON grammar, as defined in RFC 8259, allows one value per document. Only four characters count as whitespace around it: space, horizontal tab, line feed and carriage return. Once the parser closes the top-level value, anything other than those four characters is an error. Editors that use the VS Code JSON language service label this “End of file expected.” Other parsers describe the same condition in their own words. Python's json module, for example, reports “Extra data” and gives a position.

{"name": "alpha"}
{"name": "beta"}

In the example above, the first object is complete and valid. The second object is the unexpected content, so the error is flagged at its opening brace, not at the end of the file.

Common causes and how to tell them apart

Symptom where the error points | Likely cause | Distinguishing check
Start of a second { or [ on a new line | Several JSON values concatenated, or a JSON Lines (NDJSON) file opened as plain JSON | Each line parses on its own; the file extension or source is .jsonl, .ndjson, a log or a stream export
A stray } or ] near the end | Extra closing bracket, or an object that closed earlier than intended | Bracket matching in the editor shows the top-level value ending well before the last line
A semicolon, or text such as module.exports or const | JavaScript source pasted into a .json file | The content begins with an assignment or ends with a semicolon
A comma after the final closing brace | Leftover separator from copying one item out of a list | Removing the trailing comma clears the error
Apparently blank space after the value | Invisible non-JSON characters such as a non-breaking space or zero-width space | A hex view or a show-invisibles setting reveals characters other than space, tab, LF or CR
Text such as log lines, shell prompts or HTML | Output captured along with the JSON | The trailing text is clearly not data

These are plausible causes based on the JSON grammar, not a confirmed diagnosis for any specific file. The reported position is the most reliable clue. Go to that line and column and look at the first character the parser rejected.

Checks that pinpoint the problem

  • Jump to the reported line and column, and read the character there and the few before it.
  • Use the editor's bracket matching on the first opening brace to see where the parser thinks the top-level value ends.
  • Turn on visible whitespace, or open the file in a hex viewer, to catch characters outside the four permitted whitespace characters.
  • If each line is a separate object, test one line on its own. If single lines parse, the file is JSON Lines rather than one JSON document.
  • Run a second parser that reports positions, such as Python's json.tool module, to confirm the location independently.
python3 -m json.tool data.json

# For input like {"a":1}{"b":2} Python reports:
# Extra data: line 1 column 8 (char 7)

The column in that message is where the second object begins, immediately after the first object's seven characters.

Targeted fixes

Pick the fix that matches what the data is meant to be. Do not pick one only because it makes the error disappear.

  • Several records that belong together: wrap them in square brackets and separate them with commas, so they become one array.
  • A JSON Lines file: keep the one-object-per-line format and tell the editor or consuming tool to treat it as JSON Lines. Converting it to an array would break programs that expect one record per line.
  • An extra or early closing bracket: remove the stray bracket, or move it to where the structure should end, then check that nested objects still close correctly.
  • JavaScript pasted into a .json file: remove the assignment and the trailing semicolon, and keep only the literal value. JSON also needs double-quoted keys and strings and does not allow comments or trailing commas.
  • Invisible characters: delete them, or retype the affected region, then check again with visible whitespace turned on.
  • Captured log or shell output: cut everything before the first opening bracket and after the matching closing bracket.
[
  {"name": "alpha"},
  {"name": "beta"}
]

Some command-line tools, jq among them, accept a stream of concatenated JSON values and process each one in turn. That is useful for reading such files. It does not make the file a valid single JSON document for parsers that expect one value.

What formatting changes, and what it doesn't

Pretty-printing and minifying change only the whitespace between tokens. The saved file is different, but the data it describes is the same. Syntax highlighting, code folding and indentation guides only affect how the editor displays the file and leave the stored text alone. A repair step is a third, separate kind of change. It alters the actual characters and sometimes the data structure. If you run repair and formatting in one pass, compare the result with the original so you can see which edits were structural.

Some text tools offer to “reverse”, “mirror” or change the “font” of text, usually by swapping in decorative Unicode characters. These change the stored characters themselves. If they are applied to JSON keys or values, the result can still parse, but the keys will no longer match what programs look up. Keep these tools away from data you plan to parse.

Keeping JSON valid after copying and sharing

Many corrections are needed because the JSON was damaged on the way to the editor. Word processors and some chat or note apps can replace straight quotes with curly quotes or insert non-breaking spaces. Copying from a web page or terminal can pick up surrounding text, prompts or line-wrap artifacts. Sending JSON as a file attachment or inside a code block usually protects the exact characters better than pasting it into rich-text fields. That behavior depends on the destination, though, and this article does not include tests of specific apps.

For readability, an indented version is easier to review than a minified single line, especially when you are trying to locate an error position. No documentation or scoped testing was available here on how particular screen readers handle pretty-printed versus minified JSON, so that remains an open question. If someone will read the content rather than parse it, a plain-language summary or a simple table of the key fields alongside the raw JSON is the safer choice.

Limits of this guide

No product comparisons, measurements or test results were supplied for this article. The guidance relies on the JSON specification and widely documented parser behavior. Error wording and positions vary between parsers and editors. If your tool reports something different, use its documentation and the exact position it gives to confirm the cause.