request.get_jsonでJSON本文を扱う
APIクライアントからのJSONペイロードを解析します
「request.get_jsonでJSON本文を扱う」はCoddyKit上の無料Flask Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはFlask Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Flask Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
APIs Speak JSON
Modern clients often send a JSON body instead of form fields, so you need a different way to read the payload in Flask.
Call request.get_json
Use request.get_json and Flask parses the raw JSON body into a regular Python dict you can work with directly.
data = request.get_json()You Get Native Python Types
After parsing you have real Python types: dicts, lists, ints, and booleans, so no manual string conversion is needed.
price = data["price"] # already a numberRead Keys Safely
The parsed body is a dict, so reach for .get on it to avoid KeyError when an expected field is absent from the payload.
name = data.get("name")The Content-Type Must Be JSON
By default Flask only parses the body when the request carries a application/json content type header.
Force Parsing With force
If a client forgets the header, pass force=True so get_json parses the body anyway instead of returning None.
data = request.get_json(force=True)Silence Errors With silent
Bad or empty JSON normally raises a 400. Pass silent=True to get None back so you can handle it on your own terms.
data = request.get_json(silent=True)Quick Truthy Check: is_json
Branch on request.is_json first to confirm the client actually sent JSON before you try to parse the body.
if request.is_json:
data = request.get_json()Validate Before You Trust It
A parsed body is still untrusted input. Check required keys and types yourself before saving anything to your database.
if "email" not in data:
abort(400)Reply With JSON Too
Pair input parsing with jsonify so your API both reads and returns clean JSON in a single tidy handler.
from flask import jsonify
return jsonify(ok=True)A Create-User Endpoint
Together it is clean: read the JSON body, grab a field, and echo a JSON reply confirming what you received.
@app.route("/users", methods=["POST"])
def create():
data = request.get_json()
return jsonify(name=data["name"])Quick Check
Confirm how Flask handles JSON payloads.
Recap: JSON Bodies
You parsed JSON with request.get_json, controlled force and silent, checked is_json, and validated before trusting input. 🎯
よくある質問
「request.get_jsonでJSON本文を扱う」レッスンは無料ですか?
はい。「request.get_jsonでJSON本文を扱う」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Flask Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Flask Academyコースには全4レッスンが含まれています。
「request.get_jsonでJSON本文を扱う」で何を学びますか?
APIクライアントからのJSONペイロードを解析します ブラウザで直接実行するハンズオンコードでFlask Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Flask Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのFlask Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「request.get_jsonでJSON本文を扱う」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このFlask Academyレッスンでコードを書いて実行できますか?
はい。すべてのFlask Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- request.argsでクエリ文字列を扱う
- request.formでフォームフィールドを扱う
- request.get_jsonでJSON本文を扱う
- ヘッダー、Cookie、クライアントIP