SF-PD2 Advanced Developer Fundamentals Practice Question
A developer is building an Apex REST resource that must return a list of Accounts in JSON format. The resource class is defined with @RestResource(urlMapping='/Accounts/*'). The developer wants to ensure that the response includes only the Account Name and Id fields, and that the JSON structure is flat (no nested attributes). Which approach should the developer use?
⚠ Common exam trap
The trap here is assuming that querying only Id and Name with SOQL and then using JSON.serialize on the sObjects will produce a flat JSON with just those fields, but sObject serialization may include additional attributes.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
Create a custom Apex class with only Id and Name properties, populate a list of instances from the query, and serialize that list.
The requirement is to return a flat JSON structure with only Id and Name. Serializing sObjects directly can include extra metadata and fields. Defining a custom Apex class with only the needed properties and serializing a list of that class gives precise control over the output. This ensures the API contract is met without exposing unnecessary data.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
Create a custom Apex class with only Id and Name properties, populate a list of instances from the query, and serialize that list.
Why this is correct
Using a custom Apex class with only the required properties ensures that the JSON output contains exactly those fields in a flat structure. This approach gives full control over the serialized output, avoids extraneous sObject attributes, and is a common best practice for REST APIs that need a specific contract.
- ✗
Use the @JsonAccess annotation on the Account object to restrict the serialized fields to Id and Name.
Why it's wrong here
@JsonAccess is used to enforce access control on Apex properties and classes during JSON serialization and deserialization, not to limit which fields of an sObject are included. It does not provide a way to select specific fields for serialization, so it cannot achieve the desired flat structure.
- ✗
Use the JSON.serialize method on a list of Account records queried with only the Id and Name fields.
Why it's wrong here
JSON.serialize on sObjects includes all fields that are populated, and even if only Id and Name are queried, the serialized output may include additional attributes like 'attributes' with type and URL. This does not guarantee a flat JSON structure with only the desired fields, and it may expose extra data.
- ✗
Use the JSON.serializePretty method on the query results to format the JSON and then manually remove unwanted fields.
Why it's wrong here
JSON.serializePretty only adds indentation for readability; it does not remove fields. Manually removing fields from a serialized string is error-prone, inefficient, and not suitable for dynamic data. This approach does not reliably produce a flat JSON with only Id and Name.
About these practice questions
This SF-PD2 question is part of Courseiva's 226-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →
JA
Written and reviewed by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
Last reviewed September 2026 · checked against the official Salesforce exam blueprint
This SF-PD2 practice question is part of Courseiva's free Salesforce certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the SF-PD2 exam.