Pythonでjsonを扱う際に、シングルクオート「’」が含まれたjsonだとエラーになる。
import json
raw = "{'comment':'hello'}"
j = json.loads(raw)
print(j)
# json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
json.loadsのオプション設定にクオートを切り替えできそうなのがなかった。そのためastを利用して解決。
import ast
raw = "{'comment':'test comment'}"
j = ast.literal_eval(raw)
print(j)
# {'comment': 'test comment'}
参考にした記事
![](https://shinji-blog.com/wp-content/uploads/cocoon-resources/blog-card-cache/b7c9fbe6251682ab70916ac968ab6abf.jpg)
Parse 'json' with single quotes in python (Example)
A protip by k4ml about python and json.
コメント