javascript エラー「SyntaxError: JSON Parse error: Single quotes (‘) are not allowed in JSON」の解決方法
- 作成日 2022.02.27
- 更新日 2022.10.13
- javascript
- javascript

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}')
-
前の記事
MySQL カラムの名前を指定する 2022.02.27
-
次の記事
Vue.js フォーカスされたイベントを取得する 2022.02.27
コメントを書く