JSON & Serialization
Serialization is the process of converting an in-memory Python object — like a dictionary or a database model — into JSON text that can travel across the web, and deserialization is turning that text back into an object you can use.
Learn JSON & Serialization in our free Flask course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free Flask course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
In this lesson you'll serialize Python data with jsonify() , parse incoming JSON with request.get_json() , and handle tricky types like dates that aren't JSON-ready by default.
JSON is a text format for structured data. It looks almost exactly like a Python dictionary, which is why Flask can translate between the two so easily. A small JSON object might look like this:
Each JSON type lines up with a Python type. Understanding the mapping makes serialization predictable — when you pass a Python dict to jsonify() , you can picture the JSON it will produce.
jsonify() is the serializer : it takes a Python dictionary or list and returns a complete JSON response. request.get_json() is the deserializer : it reads the JSON a client sent and gives you back a Python dictionary.
The endpoint below echoes data back to the caller. It parses the incoming JSON, adds a field, and serializes the result. Run it with flask run and POST some JSON to try it.
Inside a Flask route always prefer jsonify() over json.dumps() — it sets the application/json header so clients parse the response correctly.
A custom class or a database model can't be serialized directly. The standard fix is a to_dict() method that returns only JSON-friendly fields. For a datetime , call .isoformat() to turn it into a string first.
The example below defines a User class with a join date and a to_dict() serializer, then returns it as JSON.
Complete the route so it parses JSON, builds a serializable dict, and returns it. Replace each ___ .
❌ TypeError: Object of type datetime is not JSON serializable
You passed a value JSON can't represent. Convert it first — use .isoformat() for dates, list(...) for sets, or a to_dict() method for custom objects.
You probably returned json.dumps(data) , which sends plain text. Use jsonify(data) instead so Flask sets the application/json header.
Build a route that serializes a list of Product objects into JSON.
Lesson 17 complete — you mastered JSON in Flask!
You can serialize data with jsonify() , parse it with request.get_json() , write a to_dict() serializer, and convert awkward types like dates so they're JSON-ready.
🚀 Up next: Authentication & Login — let users register, sign in, and protect routes behind a login.
Practice quiz
What is serialization?
- Encrypting a password
- Compressing a file
- Opening a database connection
- Converting an in-memory object into JSON text
Answer: Converting an in-memory object into JSON text. Serialization turns a Python object into JSON text that can travel over the web.
Which Flask function builds a complete JSON response?
- jsonify()
- render_template()
- make_json()
- to_json()
Answer: jsonify(). jsonify() serializes a dict or list into a JSON response with the right header.
How do you parse JSON a client sent in a request?
- request.form.json
- request.get_json()
- request.read_json
- request.parse()
Answer: request.get_json(). request.get_json() deserializes the incoming JSON body into a Python dict.
A Python dict serializes to which JSON type?
- A JSON string
- A JSON number
- A JSON object
- A JSON array
Answer: A JSON object. A Python dict becomes a JSON object; a list becomes a JSON array.
Why prefer jsonify() over json.dumps() inside a route?
- It is faster to type
- It validates the schema
- It sorts the keys
- It sets the application/json header so clients parse it correctly
Answer: It sets the application/json header so clients parse it correctly. jsonify() sets the Content-Type to application/json; json.dumps() returns plain text.
How do you make a custom object JSON-serializable?
- Give it a to_dict() method returning JSON-friendly fields
- Call str() on it
- Pass it straight to jsonify
- Pickle it first
Answer: Give it a to_dict() method returning JSON-friendly fields. A to_dict() method returns only JSON-friendly fields, which you then jsonify.
How do you make a datetime JSON-ready?
- Wrap it in a list
- Call .isoformat() to turn it into a string
- Call jsonify on it directly
- Convert it to bytes
Answer: Call .isoformat() to turn it into a string. datetime isn't JSON-native; .isoformat() converts it to a string.
Python True and None serialize to which JSON values?
- 1 and 0
- "True" and "None"
- true and null
- yes and no
Answer: true and null. True maps to JSON true and None maps to JSON null.
What error do you get passing a raw datetime to jsonify?
- KeyError
- SyntaxError
- ImportError
- TypeError: Object of type datetime is not JSON serializable
Answer: TypeError: Object of type datetime is not JSON serializable. JSON can't represent datetime directly, raising that TypeError.
A bonus of writing to_dict() is that you can also...
- Speed up the database
- Omit private fields like a password hash from the response
- Skip routing
- Disable CORS
Answer: Omit private fields like a password hash from the response. to_dict() lets you leave sensitive fields out of the API response.