Return Lists and Nested Data
Serialize arrays and nested objects safely.
Return Lists and Nested Data is a free Flask Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Flask Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Beyond a Single Object
Real APIs rarely return one item. They return collections and richer shapes, so you need jsonify to handle lists and nested data with ease. 📦
Lists Become JSON Arrays
A Python list maps directly onto a JSON array. Build a list of dictionaries and jsonify serializes the whole collection at once.
users = [{"name": "Ada"}, {"name": "Linus"}]Return a List of Items
Hand that list straight to jsonify and your endpoint responds with a JSON array. Each dictionary becomes one object inside the array.
@app.route("/users")
def users():
return jsonify([{"name": "Ada"}, {"name": "Linus"}])Nesting Is Natural
JSON shines at depth. A dictionary can hold another dictionary or a list, letting you model nested structures that mirror your real data.
post = {"title": "Hi", "tags": ["flask", "api"]}Deeply Nested Objects
You can nest as far as your data needs. jsonify walks the entire structure, so a dict holding a list of dicts serializes recursively with no extra effort.
data = {"team": {"members": [{"name": "Ada"}]}}Mixing Types Freely
Inside a nested structure you can blend strings, numbers, booleans, and null. jsonify converts each Python value to its correct JSON counterpart.
{"id": 7, "active": True, "score": 9.5, "note": None}Top-Level Arrays Are Fine
Older advice warned against returning a bare array, but modern Flask supports top-level JSON arrays safely. Return your list with confidence.
Wrapping for Metadata
Many teams wrap a list in a dict to add context. A results key holds the array while sibling keys carry counts or paging hints.
return jsonify(results=users, count=len(users))Only JSON-Safe Types
jsonify handles dicts, lists, strings, numbers, booleans, and None. Hand it a custom object and you will hit a serialization error instead.
Convert Objects First
When data comes from a database row or class, turn it into a plain dict before jsonify. That conversion step keeps serialization predictable and safe.
return jsonify([{"id": u.id, "name": u.name} for u in users])Order Is Preserved
Flask keeps your keys in the order you wrote them by default. That stable ordering makes responses easier to read and to compare in tests.
Quick Check
See how Python types map onto JSON when you serialize collections.
Recap
You now serialize lists and nested dicts with jsonify, converting objects to plain types first so even deep structures turn into clean JSON. 🎯
Frequently asked questions
Is the “Return Lists and Nested Data” lesson free?
Yes — the full text of “Return Lists and Nested Data” is free to read here on the web, and the Flask Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Flask Academy course, upgrade to CoddyKit PRO.
What will I learn in “Return Lists and Nested Data”?
Serialize arrays and nested objects safely. You practise Flask Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Flask Academy?
No prior experience is required. Flask Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Return Lists and Nested Data” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Flask Academy lesson?
Yes. Every Flask Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- jsonify a Dictionary
- Return Lists and Nested Data
- Set Status Codes on JSON Replies
- Content-Type and the Accept Header