0
0
Rest APIprogramming~20 mins

Sparse fieldsets (select fields) in Rest API - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Sparse Fieldsets Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this REST API response when using sparse fieldsets?

Given the API request GET /articles?fields[articles]=title,author, what fields will the response JSON include for each article?

Rest API
{
  "data": [
    {"id": "1", "type": "articles", "attributes": {"title": "JSON API paints my bikeshed!", "body": "The shortest article ever.", "author": "John"}},
    {"id": "2", "type": "articles", "attributes": {"title": "Rails is Omakase", "body": "Rails is a web-application framework.", "author": "Jane"}}
  ]
}
A{"data":[{"id":"1","type":"articles","attributes":{"title":"JSON API paints my bikeshed!","body":"The shortest article ever."}},{"id":"2","type":"articles","attributes":{"title":"Rails is Omakase","body":"Rails is a web-application framework."}}]}
B{"data":[{"id":"1","type":"articles","attributes":{"body":"The shortest article ever.","author":"John"}},{"id":"2","type":"articles","attributes":{"body":"Rails is a web-application framework.","author":"Jane"}}]}
C{"data":[{"id":"1","type":"articles","attributes":{"title":"JSON API paints my bikeshed!","author":"John"}},{"id":"2","type":"articles","attributes":{"title":"Rails is Omakase","author":"Jane"}}]}
D{"data":[{"id":"1","type":"articles","attributes":{"title":"JSON API paints my bikeshed!"}},{"id":"2","type":"articles","attributes":{"title":"Rails is Omakase"}}]}
Attempts:
2 left
💡 Hint

Look at the fields[articles] parameter and which attributes it selects.

🧠 Conceptual
intermediate
1:30remaining
Why use sparse fieldsets in REST API responses?

What is the main benefit of using sparse fieldsets (select fields) in REST API responses?

ATo reduce the amount of data sent over the network by including only needed fields
BTo encrypt the response data for security
CTo force the client to request all fields explicitly every time
DTo increase the number of fields returned for better debugging
Attempts:
2 left
💡 Hint

Think about network speed and data size.

🔧 Debug
advanced
1:30remaining
Identify the error in this sparse fieldset query parameter

Which option shows the correct sparse fieldset syntax to request only the name and email fields for users resource?

Rest API
GET /users?fields[users]=name,email
AGET /users?fields[users]:name,email
BGET /users?fields[users]=name,email
CGET /users?fields(users)=name,email
DGET /users?fields=users(name,email)
Attempts:
2 left
💡 Hint

Check the correct syntax for specifying fields in JSON API query parameters.

Predict Output
advanced
2:00remaining
What is the output when requesting sparse fieldsets with an invalid field?

Given the request GET /articles?fields[articles]=title,summary but the summary field does not exist in the API, what is the expected behavior or output?

A{"errors":[{"status":"400","title":"Invalid field","detail":"Field 'summary' is not recognized for resource 'articles'."}]}
B{"data":[{"id":"1","type":"articles","attributes":{"title":"JSON API paints my bikeshed!"}}]}
C{"data":[{"id":"1","type":"articles","attributes":{"title":"JSON API paints my bikeshed!","summary":null}}]}
D{"data":[{"id":"1","type":"articles","attributes":{"title":"JSON API paints my bikeshed!","summary":""}}]}
Attempts:
2 left
💡 Hint

Consider how APIs handle requests for unknown fields.

🚀 Application
expert
2:30remaining
How to combine sparse fieldsets with includes to optimize API response?

You want to request articles with only the title and author fields, and include the comments relationship but only the body field of comments. Which query string correctly achieves this?

AGET /articles?include=comments&fields[articles]=title,author,comments&fields[comments]=body
BGET /articles?fields=title,author&include=comments&fields=body
CGET /articles?fields[articles]=title,author&include=comments&fields[comments]=body,author
DGET /articles?fields[articles]=title,author&include=comments&fields[comments]=body
Attempts:
2 left
💡 Hint

Remember to specify fields separately for each resource type.