javascript エラー「SyntaxError: JSON Parse error: Single quotes (‘) are not allowed in JSON」の解決方法

javascript エラー「SyntaxError: JSON Parse error: Single quotes (‘) are not allowed in JSON」の解決方法

javascriptで、エラー「SyntaxError: JSON Parse error: Single quotes (‘) are not allowed in JSON」が発生した場合の原因と解決方法を記述してます。

環境

  • OS macOS Monterey
  • ブラウザ safari 15.5

エラー内容

以下のコードを実行時に発生。

JSON.parse("{'bar': 1}")

エラーメッセージ

SyntaxError: JSON Parse error: Single quotes (') are not allowed in JSON

画像

firefox(バージョン97)では、以下のエラーとなります。

Uncaught SyntaxError: JSON.parse: expected property name or '}' at line 1 column 2 of the JSON data

画像

原因

プロパティに対してはシングルクォーテーションは使用できないため

解決方法

ダブルクォーテーションを使用する

JSON.parse('{"bar": 1}')