How to get POSTed JSON in Flask?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Flask, the normal way to read posted JSON is request.get_json(). That gives you parsed JSON from the request body as long as the client sent valid JSON with the right content type. The rest of the work is not parsing but validating the payload shape and returning clear errors when the client sends something malformed.
Use request.get_json() as the Primary API
A basic route that accepts JSON looks like this.
You can test it with curl.
Using silent=True is convenient when you want invalid JSON to produce None instead of raising immediately. That lets your route return a consistent error response under your control.
Check the Content Type Intentionally
If the endpoint is supposed to accept JSON only, it is worth checking the content type before deeper validation.
This makes the API contract clear. A bad content type is a different client error from malformed JSON.
Parsing Is Not Validation
A JSON body can be syntactically valid and still be wrong for your application. After parsing, you should validate required fields, types, and business rules.
This separation keeps the route logic much easier to reason about:
- first parse,
- then validate,
- then act.
Nested JSON Needs Careful Access
Nested request bodies are common, but direct indexing can turn a client mistake into a server exception.
This is safer than chaining direct dictionary lookups and hoping every nested field exists.
Prefer Flask Parsing Over Manual json.loads(request.data)
You can manually parse the request body yourself, but Flask already gives you a request-aware JSON API. request.get_json() handles the common case more cleanly and makes the intent obvious.
Manual parsing is usually only necessary when you have unusual content-handling rules. For standard JSON API endpoints, the built-in method is the better default.
Common Pitfalls
- Using
request.dataand manual JSON parsing whenrequest.get_json()already solves the problem cleanly. - Assuming posted JSON is always a dictionary. It can be another valid JSON type.
- Accepting malformed or non-JSON requests without distinguishing between content-type errors and payload errors.
- Accessing nested keys directly and turning bad client input into server exceptions.
- Stopping at parsing and forgetting to validate fields, types, and required structure.
Summary
- In Flask, use
request.get_json()to read posted JSON. - Check
request.is_jsonwhen the endpoint should only acceptapplication/json. - Treat parsing and validation as separate steps.
- Access nested data carefully so client mistakes do not crash the route.
- Use Flask’s built-in request parsing before reaching for manual
json.loadslogic.
Related reading
- How to get precision, recall and f-measure from confusion matrix in Python
- How to get Python requests to trust a self signed SSL certificate?
- How to get sample indices from RandomUnderSampler in imblearn
- How to get skbio PCoA Principal Coordinate Analysis results?
- How to get string objects instead of Unicode from JSON
- How to get subprocess' stdout data asynchronously?
- How to get Tensorflow tensor dimensions shape as int values?
- How to get Tensorflow tensor dimensions shape as int values?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.