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モジュールを使用してJSON文字列を整形

Posted at

json.loads: JSON文字列をPythonオブジェクト(辞書やリストなど)に変換します。
json.dumps: PythonオブジェクトをJSON文字列に変換します。
indent=4: インデントを4スペースで整形します。
ensure_ascii=False: 日本語などの非ASCII文字をそのまま出力します。

jsonsample.py
import json

# 元のJSON文字列
json_str = '{"name":"Alice","age":25,"hobbies":["reading","cycling"],"is_student":false}'

# JSON文字列をPythonオブジェクトに変換
data = json.loads(json_str)

# 整形して出力
pretty_json = json.dumps(data, indent=4, ensure_ascii=False)
print(pretty_json)
{
    "name": "Alice",
    "age": 25,
    "hobbies": [
        "reading",
        "cycling"
    ],
    "is_student": false
}
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?