DevToolBox

JSONの "Unexpected token" エラーを直す

JSON.parseSyntaxError: Unexpected token ... in JSON at position Nが出たときの原因を、position N の読み方から系統立てて解説します。

This guide explains why JSON.parsethrows "Unexpected token ... at position N" and walks through the most common causes (trailing commas, single quotes, undefined, BOM, NaN).

TL;DR

1. position N の読み方 / How to read "position N"

position の N は、入力文字列の 0始まりのコードポイントインデックス。 改行・タブ・BOM も 1 文字としてカウントされます。VS Code なら Ctrl+G → 目的行へジャンプし、 列番号を N から換算すれば一発で特定できます。

N is a zero-based character index, counting newlines and BOM. Jump to it in your editor.

2. 代表的な原因と対処 / Common causes

症状 / Symptom原因 / Cause対処 / Fix
Unexpected token , in JSON at position N末尾カンマ / Trailing comma最後のカンマを削除 / Remove it
Unexpected token ' in JSONシングルクォート / Single quotes全てダブルクォートに / Use double quotes
Unexpected token u in JSON at position 0値が undefined / Value is undefinedresponse ?? "null" で fallback
Unexpected token in JSON at position 0 (不可視)BOM / UTF-8 BOMs.replace(/^\uFEFF/, "")
Unexpected token N in JSONNaN / Infinity を書いた / Non-JSON numbersnull に置換 / Replace with null
Unexpected end of JSON input閉じ括弧不足 / Truncatedログ切れなら再取得 / Re-fetch source

3. 再発防止 / Prevention

4. English summary

JSON.parse throws Unexpected token ... in JSON at position Nwhen the input contains something that isn't legal JSON. The position is a zero-based character index, counting newlines and BOM. The frequent offenders are trailing commas, single quotes, a literalundefined, a UTF-8 BOM at position 0, and NaN/Infinitywhich JSON doesn't allow. Jump to the index in your editor, identify the category, and apply the corresponding fix. For recurring issues, validate payloads with a JSON Schema.

関連ツール / Related tools

関連ガイド / Related guides