0
0
Rest APIprogramming~30 mins

Resource expansion (embed related data) in Rest API - Mini Project: Build & Apply

Choose your learning style9 modes available
Resource Expansion (Embed Related Data) in REST API
📖 Scenario: You are building a simple REST API for a bookstore. Each book has an author, and you want to send book data along with the author's details in one response. This is called resource expansion or embedding related data.
🎯 Goal: Build a REST API endpoint that returns a list of books with their embedded author information.
📋 What You'll Learn
Create a list of books with author IDs
Create a list of authors with their details
Write code to expand each book's author ID to the full author data
Return the expanded book list as JSON
💡 Why This Matters
🌍 Real World
Embedding related data in API responses helps clients get all needed information in one request, improving speed and user experience.
💼 Career
Understanding resource expansion is important for backend developers building efficient and user-friendly REST APIs.
Progress0 / 4 steps
1
Create the initial data structures
Create a list called books with these exact entries: {"id": 1, "title": "The Great Gatsby", "author_id": 101}, {"id": 2, "title": "1984", "author_id": 102}, and a list called authors with these exact entries: {"id": 101, "name": "F. Scott Fitzgerald"}, {"id": 102, "name": "George Orwell"}.
Rest API
Need a hint?

Use lists of dictionaries exactly as shown. Each book has an author_id that matches an id in authors.

2
Create a helper dictionary for authors
Create a dictionary called author_map that maps each author's id to the full author dictionary from the authors list.
Rest API
Need a hint?

Use a dictionary comprehension to map author IDs to author data.

3
Expand books with embedded author data
Create a new list called expanded_books that contains each book dictionary plus a new key author with the full author dictionary from author_map. Use a for loop with variables book and expanded_books.
Rest API
Need a hint?

Copy each book dictionary, add the author data, then add to expanded_books.

4
Print the expanded books list
Write a print statement to display the expanded_books list.
Rest API
Need a hint?

Use print(expanded_books) to show the final list with embedded author data.