Unclosed bracket in JSON: “Unexpected end of JSON input”
Updated
Every bracket that opens must close. When the input ends with an object or an array still open, the parser reaches the end of the text while waiting for } or ]. It can only report the end, which is rarely where the bracket went missing.
Unclosed object: a missing }
The last line of an object was lost: cut off by a copy, a log line limit or a response that stopped early.
{
"id": "op-0042",
"team": [
{"callsign": "Falcon"},
{"callsign": "Owl"}
]The array closed, the object didn’t. One } at the end fixes it:
{
"id": "op-0042",
"team": [
{"callsign": "Falcon"},
{"callsign": "Owl"}
]
}Unclosed array: a missing ]
The same thing with an array: a list of events written to a file or a log, and the writer stopped before the end. Here the array is the innermost bracket still open when the input ends:
[
{"event": "start", "at": 1},
{"event": "stop", "at": 2}Add ] after the last element:
[
{"event": "start", "at": 1},
{"event": "stop", "at": 2}
]If something does follow, for example } arrives while [ is still open as in {"tags": ["quiet", "fast"}, the input hasn’t ended and the error is a mismatched bracket instead. The fix is the same: put ] where the list ends.
Why the error points at the end
RFC 8259 defines an object as begin-object [ members ] end-object and an array as begin-array [ values ] end-array: every container is closed by its own bracket. Nothing in JSON marks the end of a container except that bracket, so when it is missing the parser keeps reading members until the text runs out. Only then is the document provably broken, and the end of the input is the only position it can name.
That is why the useful number is not where the error was detected but where the open bracket is. This site’s validator reports both: the end of the input and “Opened at line L, column C” for the innermost bracket that never closed.
Unclosed bracket error messages by parser
An object cut after a value, {"a": 1, and an array cut after an element, [1, 2:
| Parser | Object: {"a": 1 | Array: [1, 2 |
|---|---|---|
| Chrome, Edge, Node.js (V8) | Expected ',' or '}' after property value in JSON at position 7 (line 1 column 8) | Expected ',' or ']' after array element in JSON at position 5 (line 1 column 6) |
| Node.js 18, older Chrome (V8 10.2) | Unexpected end of JSON input | Unexpected end of JSON input |
| Firefox | JSON.parse: end of data after property value in object at line 1 column 8 of the JSON data | JSON.parse: end of data when ',' or ']' was expected at line 1 column 6 of the JSON data |
| Safari | JSON Parse error: Expected '}' | JSON Parse error: Expected ']' |
| Python | Expecting ',' delimiter: line 1 column 8 (char 7) | Expecting ',' delimiter: line 1 column 6 (char 5) |
| Go (encoding/json) | unexpected end of JSON input | unexpected end of JSON input |
| Java (Jackson 2) | Unexpected end-of-input: expected close marker for Object, with the line and column where it opened | Unexpected end-of-input: expected close marker for Array, with the line and column where it opened |
| C# (System.Text.Json) | '1' is an invalid end of a number. Expected a delimiter. | '2' is an invalid end of a number. Expected a delimiter. |
| C# (Newtonsoft.Json) | Unexpected end of content while loading JObject. Path 'a', line 1, position 7. | Unexpected end of content while loading JArray. Path '[1]', line 1, position 5. |
| PHP (json_decode) | Syntax error, json_last_error() 4 | Syntax error, json_last_error() 4 |
| Rust (serde_json) | EOF while parsing an object at line 1 column 7 | EOF while parsing a list at line 1 column 5 |
| jq | jq: parse error: Unfinished JSON term at EOF at line 1, column 7 | jq: parse error: Unfinished JSON term at EOF at line 1, column 5 |
Where the input is cut changes the wording. Cut right after a colon or a comma, {"a": or [1, 2,, and current V8 also says Unexpected end of JSON input. Ending with a closed value, {"a": [1, 2], System.Text.Json says Expected depth to be zero at the end of the JSON payload. There is an open JSON object or array that should be closed. Current V8 and Python never mention the end of input for {"a": 1: they report the comma they were still waiting for. Checked by running: Node.js 18, 24 and 25.1, Safari 27, Python 3.9 and 3.14, Go 1.25–1.27, Jackson 2.22 and 3.2, System.Text.Json and Newtonsoft.Json 13.0.4, PHP 8.3 and 8.5, serde_json 1.0, jq 1.7 and 1.8; Firefox from its source code and Mozilla’s reference.
Missing bracket or cut-off input?
- Cut-off input ends in the middle of something: a value, a key, a half-written string. Compare the length you received with what was sent (the
Content-Lengthheader, the file size) and look for limits on the way: log line length, a terminal buffer, a message size in a queue. - A forgotten bracket leaves a complete-looking document that is short of one
}or]. Format the document and follow the indentation: the place where it stops matching the structure is where the bracket belongs. - The reported bracket is the innermost open one, not necessarily the one you forgot. If
{"a": {"b": 1, "c": 2}lost the inner}after1, the parser closes the inner object at the end and reports the outer one. - An empty response is not an unclosed bracket at all. That is Unexpected end of JSON input with nothing to parse.
How to find an unclosed bracket in code
Two useful questions: did the error happen at the very end of the input (cut off or a missing bracket), and which bracket is still open. The JavaScript and PHP snippets answer the second one with a small scan that skips strings.
JavaScript
// Innermost bracket that never closed, or null. Brackets inside strings are skipped.
function unclosed(text) {
const stack = [];
let inString = false;
for (let i = 0; i < text.length; i++) {
const c = text[i];
if (inString) {
if (c === "\\") i++;
else if (c === '"') inString = false;
} else if (c === '"') inString = true;
else if (c === "{" || c === "[") stack.push(i);
else if (c === "}" || c === "]") stack.pop();
}
if (stack.length === 0) return null;
const lines = text.slice(0, stack.at(-1)).split("\n");
return { bracket: text[stack.at(-1)], line: lines.length, column: lines.at(-1).length + 1 };
}
console.log(unclosed('{\n "team": [\n {"callsign": "Owl"}')); // { bracket: '[', line: 2, column: 11 }Python
import json
text = '{"team": [{"callsign": "Owl"}'
try:
json.loads(text)
except json.JSONDecodeError as e:
if e.pos == len(text):
print("the input ends too early: cut off or missing } or ]")
print(e.msg, e.pos) # Expecting ',' delimiter 29Go
package main
import (
"encoding/json"
"errors"
"fmt"
"io"
"strings"
)
func main() {
// A Decoder reading a stream tells a cut-off document apart: io.ErrUnexpectedEOF.
dec := json.NewDecoder(strings.NewReader(`{"team": [{"callsign": "Owl"}`))
var v any
err := dec.Decode(&v)
fmt.Println(errors.Is(err, io.ErrUnexpectedEOF), err) // true unexpected EOF
}Java (Jackson)
import com.fasterxml.jackson.core.io.JsonEOFException;
import com.fasterxml.jackson.databind.ObjectMapper;
try {
new ObjectMapper().readTree("{\"team\": [{\"callsign\": \"Owl\"}");
} catch (JsonEOFException e) {
// Its own exception type for input that ends too early.
System.out.println(e.getOriginalMessage()); // Unexpected end-of-input: expected close marker for Array
}C#
using System.Text.Json;
// isFinalBlock: false lets the reader stop at the end instead of throwing.
var reader = new Utf8JsonReader("{\"a\": [1, 2]"u8, isFinalBlock: false, state: default);
while (reader.Read()) { }
Console.WriteLine(reader.CurrentDepth); // 1: one bracket is still openPHP
function unclosed(string $text): ?array {
$stack = [];
$inString = false;
for ($i = 0; $i < strlen($text); $i++) {
$c = $text[$i];
if ($inString) {
if ($c === '\\') $i++;
elseif ($c === '"') $inString = false;
} elseif ($c === '"') $inString = true;
elseif ($c === '{' || $c === '[') $stack[] = $i;
elseif ($c === '}' || $c === ']') array_pop($stack);
}
if ($stack === []) return null;
$at = end($stack);
return [$text[$at], substr_count($text, "\n", 0, $at) + 1]; // bracket, line
}
print_r(unclosed("{\n \"team\": [\n {\"callsign\": \"Owl\"}"));How to keep brackets closed
- Read a response to the end before parsing it; don’t parse a buffer that may still be filling.
- Log JSON on one line through a serializer, and raise log line limits for payloads you need to read back.
- When editing by hand, type the closing bracket together with the opening one, then fill in the middle.
- Paste suspicious text into the JSON validator: it shows where the open bracket is, not only where the text ended.
Try it in the validator
Open a sample with an unclosed object or an unclosed array in the JSON validator. The error card reports unclosed_object or unclosed_array at the end of the input, and its “Opened at” line is a button that puts the cursor on the bracket that never closed.
Related: mismatched bracket, unclosed string, Unexpected end of JSON input, missing comma, and all JSON errors.
Unclosed bracket FAQ
What does “Unexpected end of JSON input” mean?
The text ended while the parser still expected more: a value, a closing bracket or a closing quote. Either the input is empty or cut off, or a bracket was never closed.
Why is the error on the last line when the bracket is missing much earlier?
JSON has no other marker for the end of an object or array than its bracket, so the parser reads on until the text runs out. Look for the line where the opening bracket is, not the line of the error.
How do I find which bracket is not closed?
Format the document and follow the indentation, or use a validator that reports the open bracket. This site’s validator shows “Opened at line L, column C” for it.
Can a parser just add the missing bracket?
It could guess, and guesses change data: the bracket might belong after any of the remaining members. That is why strict parsers refuse and leave the decision to you.