"Unexpected end of JSON input" の直し方
JSON.parse や res.json() でSyntaxError: Unexpected end of JSON input が出るのは、JSONとして完結する前に入力が終わったときです。実際の原因はほぼ 「空文字列」「空レスポンス」「途中で切れたJSON」の3つに絞れます。
This guide explains why JSON.parsethrows "Unexpected end of JSON input" and walks through the three real-world causes: empty strings, empty response bodies, and truncated JSON.
TL;DR
- 最頻出は
JSON.parse("")。localStorage の未保存キーや空のAPIボディが典型 - fetch なら 204 No Content・エラー時の空ボディを疑う。
res.json()の前にステータス確認 - ログや文字列連結で JSONが途中で切れていないかはJSON整形ツールに貼ると一瞬で分かる
1. エラーの意味 / What the error means
パーサーは { や [ を読み始めると、対応する閉じ記号まで読み切ろうとします。 その途中で入力が尽きると「end of input(入力の終端)に予期せず到達した」としてこのエラーになります。 つまり壊れた文字があるのではなく、続きが無いのが特徴です (壊れた文字がある場合は Unexpected token エラーになります)。
The parser reached the end of the string while a JSON value was still open. Nothing is malformed — the rest of the document is simply missing.
2. 原因別の対処 / Causes and fixes
| 状況 / Situation | 原因 / Cause | 対処 / Fix |
|---|---|---|
JSON.parse(localStorage.getItem(...)) | 値が ""(空文字列) / Empty string | v ? JSON.parse(v) : null でガード |
res.json() が reject する | 204/205 や DELETE 成功時の空ボディ / Empty body | ステータスで分岐してから parse |
| エラー時だけ落ちる | サーバーが 500 で空ボディを返す / Empty error body | res.ok を先に確認 |
| ログから拾ったJSONで発生 | 出力が文字数制限で切れた / Truncated log | 元データを再取得(ログは信用しない) |
| 大きなファイルの読み込みで発生 | 書き込み未完了・転送中断 / Incomplete write | 生成側で flush/close を確認 |
| ストリーミング受信で発生 | チャンク単位で逐次 parse している / Parsing partial chunks | 全チャンク連結後に parse(または NDJSON 化) |
3. fetch の安全な書き方 / Safe fetch pattern
const res = await fetch(url);
if (!res.ok) {
throw new Error(`HTTP ${res.status}`);
}
// 204 や空ボディでも落ちない
const text = await res.text();
const data = text ? JSON.parse(text) : null;res.json() を直接呼ばず一度 text() で受けてから判定するのが 最も堅牢です。ステータスコードの意味はHTTPステータスコード検索で確認できます。
4. ブラウザ・環境ごとの文言差 / Error wording by environment
- Chrome / Node.js:
Unexpected end of JSON input - Firefox:
JSON.parse: unexpected end of data at line 1 column 1 - Safari:
JSON Parse error: Unexpected EOF
文言は違っても原因と対処は同じです。検索するときはこのページの3原因(空文字列・空ボディ・途中切れ)を順に当てはめてください。
5. English summary
JSON.parsethrows "Unexpected end of JSON input" when the input ends while a JSON value is still open. In practice the cause is almost always one of three things: parsing an empty string (e.g. a missing localStorage key), calling response.json() on an empty body (204 No Content, DELETE responses, or error responses with no payload), or JSON that was truncated by a log line limit or an incomplete file write. Guard withconst text = await res.text(); const data = text ? JSON.parse(text) : null; and checkres.okbefore parsing. Firefox reports the same problem as "unexpected end of data" and Safari as "Unexpected EOF".
関連ツール / Related tools
- JSON Formatter & Validator - 貼り付けるだけで切れている位置を特定
- HTTP Status Code 検索
- JSON Schema Validator