Courseiva

SF-PD2 Advanced Developer Fundamentals Practice Question

A developer is building an Apex REST resource that must accept an inbound JSON payload from a third-party system. The payload contains a nested structure with a list of order line items, each having its own nested product object. The developer wants to deserialize the payload directly into typed Apex classes without writing manual parsing logic. Which approach should the developer use?

⚠ Common exam trap

The trap here is assuming JSON.deserializeUntyped can be cast to a typed Apex class, when it actually returns Map and List instances that cannot be cast to custom types.

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

✓

Call JSON.deserialize with the typed Apex class as the second argument, ensuring the class hierarchy mirrors the JSON structure.

Typed deserialization with JSON.deserialize against an Apex class whose members mirror the JSON keys is the standard way to convert a structured payload into Apex objects. Nested lists and inner classes are populated automatically by matching names, so no manual token handling is needed. The untyped and parser-based approaches require explicit mapping and therefore reintroduce the manual work the scenario wants to eliminate.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✗

    Use JSONParser to walk the token stream and instantiate each typed class as tokens are read.

    Why it's wrong here

    JSONParser is a lower-level tokenizer intended for cases where you must inspect or conditionally process a stream. Using it requires explicitly reading each token, tracking nesting, and constructing every object yourself, which is exactly the manual parsing logic the developer wants to avoid. It works, but it is the wrong tool when the JSON shape is known and already modeled by Apex classes.

  • ✗

    Use JSON.deserializeUntyped and cast the returned Object directly to the typed Apex class.

    Why it's wrong here

    JSON.deserializeUntyped returns Object instances backed by Map<String,Object> and List<Object>, never instances of your custom Apex class. A direct cast to the typed class throws a runtime type exception because the underlying runtime type is a generic collection, not the declared Apex type. It requires manual key-by-key mapping to populate typed objects, so it does not meet the requirement of avoiding manual parsing logic.

  • ✗

    Use JSON.createParser and bind the parser to a typed Apex class using the readValueAs method.

    Why it's wrong here

    There is no binding of a JSONParser to a typed Apex class in Apex; readValueAs is not a method used to hydrate arbitrary Apex classes from a parser stream in this manner. JSONParser exposes token navigation such as nextToken and getCurrentToken, not typed object materialization. This option invents behavior that does not exist, so it cannot satisfy the requirement.

  • ✓

    Call JSON.deserialize with the typed Apex class as the second argument, ensuring the class hierarchy mirrors the JSON structure.

    Why this is correct

    JSON.deserialize(jsonString, apexType) maps JSON properties to Apex class members by name, so a class hierarchy that mirrors the nested JSON deserializes in a single call without manual traversal. Nested lists and inner objects are handled automatically as long as each inner type is declared, making this the intended approach for typed, structured payloads in Apex REST resources.

About these practice questions

Courseiva writes every SF-PD2 question from scratch — 226 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

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.