0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Python初心者】JSONデータの変換とファイル読み書きの基本

Last updated at Posted at 2025-06-19

PythonでJSONデータを扱う方法について学んだので、記録としてまとめておきます。
この回では、Pythonの json モジュールを使って、辞書やリストとJSON文字列との変換、およびファイルへの読み書きの方法を確認しています。

jsonモジュールを使う準備

Pythonでは標準ライブラリの json モジュールを使うことで、JSON形式のデータを簡単に扱うことができます。

import json

追加のインストールは不要です。

PythonのデータをJSON文字列に変換する(json.dumps)

辞書やリストなどのPythonオブジェクトを、JSON形式の文字列に変換するには json.dumps() を使います。

import json

data = {"name": "Alice", "age": 30, "city": "New York"}

json_str = json.dumps(data)

print(json_str)       # JSON形式の文字列として出力されます
print(type(json_str)) # 型を確認します
{"name": "Alice", "age": 30, "city": "New York"}
<class 'str'>

type() を使うことで、Pythonの辞書がJSON形式の文字列に変換されたことが明確にわかります。

整形して出力する(インデント付き)

見やすく整形されたJSON文字列を出力したい場合は、indent 引数を指定します。

json_str = json.dumps(data, indent=2)
print(json_str)
{
  "name": "Alice",
  "age": 30,
  "city": "New York"
}

JSON文字列をPythonのデータに変換する(json.loads)

JSON形式の文字列をPythonの辞書などに変換するには json.loads() を使います。

import json

json_str = '{"name": "Bob", "age": 25, "city": "Los Angeles"}'

data = json.loads(json_str)
print(data)
print(type(data))  # dict 型になっていることを確認
{'name': 'Bob', 'age': 25, 'city': 'Los Angeles'}
<class 'dict'>

JSONファイルに書き込む(json.dump)

Pythonのデータを直接ファイルに保存するには、json.dump() を使います。

import json

data = {"name": "Alice", "age": 30, "city": "New York"}

with open("data.json", "w", encoding="utf-8") as f:
    json.dump(data, f, indent=2)
# data.json に書き込まれる内容(整形済み)
{
  "name": "Alice",
  "age": 30,
  "city": "New York"
}

JSONファイルを読み込む(json.load)

JSONファイルからデータを読み込むには、json.load() を使います。

import json

with open("data.json", "r", encoding="utf-8") as f:
    data = json.load(f)

print(data)
print(type(data))  # dict 型であることを確認
{'name': 'Alice', 'age': 30, 'city': 'New York'}
<class 'dict'>

dump / dumps、load / loads の違い

関数名に s があるかどうかで、扱う対象が異なります。どちらも意味は似ていますが、使い方が明確に分かれているので注意が必要です。

関数名 処理内容 対象 備考
dump() Pythonのデータ → JSONとしてファイルに保存 ファイルオブジェクト with open(...) と一緒に使います
dumps() Pythonのデータ → JSON形式の文字列に変換 文字列 s = string という意味で覚えるとわかりやすいです
load() JSONファイル → Pythonのデータに変換 ファイルオブジェクト ファイルからデータを読み込むときに使います
loads() JSON形式の文字列 → Pythonのデータに変換 文字列 こちらも s = string です

s の有無で覚えるコツ

  • s がある → "string"(文字列) を扱う関数
  • s がない → ファイル を扱う関数

この区別が意外と重要だったので、自分の中で明確にしておきたいと思いました。

まとめ

  • json.dumps():PythonのデータをJSON形式の文字列に変換します。
  • json.loads():JSON文字列をPythonのデータに変換します。
  • json.dump():PythonのデータをJSONファイルに保存します。
  • json.load():JSONファイルを読み込んでPythonのデータとして扱います。
  • type() を使うと、データ型の変化が確認できて理解しやすいです。
  • s の有無で「文字列」か「ファイル」かが分かれる点に注意します。

辞書やリストとJSONとの相互変換は、データのやりとりにおいてよく使われるので、使い分けをしっかり覚えておきたいです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?