JSON データを扱うとき、末尾のコンマ、中かっこの不一致、不適切なデータ型など、JSON が無効になる問題が発生することがあります。 ギットハブ コパイロット チャット は、無効な 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
}
}
プロンプトの例
Why is my JSON object invalid and how can I fix it?
{
"location": "San Francisco",
"current_weather": {
"temperature": 18,
"unit": "Celsius",
"conditions": "Cloudy
},
"forecast": {
"day": "Monday",
"high": 22,
"low": 15,
"precipitation": 10
}
}
Why is my JSON object invalid and how can I fix it?
Why is my JSON object invalid and how can I fix it?
応答の例
メモ
次の応答は例です。 コパイロットチャット の応答は非決定的であるため、ここで示したものとは異なる応答が返される可能性があります。
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の提案には、JSON 解析エラーを解決する conditions 値の終了引用符の修正が含まれます。