Skip to main content
Select language: current language is Japanese
検索する、または Copilot に質問する
メニューを開く

無効な JSON のデバッグ

Copilot チャット は、JSON データの構文エラーや構造上の問題を特定して解決できます。

この記事で

JSON データを扱うとき、末尾のコンマ、中かっこの不一致、不適切なデータ型など、JSON が無効になる問題が発生することがあります。 GitHub Copilot チャット による、無効な JSON を解決するための修正の提案は、これらのエラーのデバッグと修正に役立ちます。

シナリオ例

アプリケーションが API から JSON データを使用しているものの、無効な書式設定が原因で応答の解析が失敗するシナリオを考えてみます。 次のようなエラー メッセージを受け取ります。

Error: Parse error
----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined'

エラーの原因になったのは次の JSON データです。

{
  "location": "San Francisco",
  "current_weather": {
    "temperature": 18,
    "unit": "Celsius",
    "conditions": "Cloudy
  },
  "forecast": {
    "day": "Monday",
    "high": 22,
    "low": 15,
    "precipitation": 10
  }
}

プロンプトの例

Copilot prompt
Why is my JSON object invalid and how can I fix it?

応答の例

メモ

次の応答は例です。 Copilot チャット の応答は非決定的であるため、ここで示したものとは異なる応答が返される可能性があります。

Copilot は、conditions 値に終了引用符がないため JSON が無効であることを示唆する可能性があります。 修正後の JSON は次のようになります。

{
  "location": "San Francisco",
  "current_weather": {
    "temperature": 18,
    "unit": "Celsius",
    "conditions": "Cloudy"
  },
  "forecast": {
    "day": "Monday",
    "high": 22,
    "low": 15,
    "precipitation": 10
  }
}

この応答例での Copilot の提案には、conditions 値の終了引用符の修正が含まれ、それによって JSON 解析エラーは解決します。

参考資料

Morty Proxy This is a proxified and sanitized view of the page, visit original site.