# PythonでJSON文字列をdictに変換する

PythonでJSON文字列をdictに変換するには、Python 標準ライブラリのjson.loads(JSON文字列)を使う。

ヒアドキュメント('''で複数業の文字列を囲む)によりJSON文字列を宣言し、json.loads()でdictに変換する。
読み込んだものが文字列、数値に変換されていることを確認する。

json_test.py

import json

json_string = '''
{
  "name": "nancy",
  "gender": "male",
  "age": 99
}
'''

json_dict = json.loads(json_string)
print(type(json_dict))
print(f"name:{json_dict['name']}, type:{type(json_dict['name'])}")
print(f"age:{json_dict['age']}, type:{type(json_dict['age'])}")
$ python json_test.py
<class 'dict'>
name:nancy, type:<class 'str'>
age:99, type:<class 'int'>

もしJSON文字列が正しい形式でない場合、json.decoder.JSONDecodeErrorが発生する。
したがって、エラーをハンドリングするにはtry/exceptでjson.loadsの処理をくくる必要がある。

次のコードはJSON文字列から,を一つ少なくしているため、JSON文字列からdictへデコードできず、エラーになる。

import json

json_string = '''
{
  "name": "nancy"
  "gender": "male",
  "age": 99
}
'''
try:
  json_dict = json.loads(json_string)
  print(type(json_dict))
  print(f"name:{json_dict['name']}, type:{type(json_dict['name'])}")
  print(f"age:{json_dict['age']}, type:{type(json_dict['age'])}")
except json.decoder.JSONDecodeError as error:
  print(error)
$ python json_test.py 
Expecting ',' delimiter: line 4 column 3 (char 23)