Bird
0
0

Which of the following templates correctly implements this logic?

hard📝 Application Q9 of 15
AWS - API Gateway
You have a response mapping template that needs to conditionally include an error field only if the backend response contains an error message. Which of the following templates correctly implements this logic?
A#if($input.path('$.errorMessage')) {"error": "$input.path('$.errorMessage')"} #else {} #end
B{ "error": "$input.path('$.errorMessage')" }
C#set($error = $input.path('$.errorMessage')) {"error": "$error"}
D#if($input.path('$.errorMessage') == null) {"error": "No error"} #end
Step-by-Step Solution
Solution:
  1. Step 1: Understand conditional logic in templates

    Use #if directive to check if errorMessage exists before including error field.
  2. Step 2: Evaluate options

    #if($input.path('$.errorMessage')) {"error": "$input.path('$.errorMessage')"} #else {} #end includes error only if errorMessage exists. The simple JSON object always includes error (null if missing). The #set version always includes error even if null. The null-check version includes error only when missing, with wrong message.
  3. Final Answer:

    #if($input.path('$.errorMessage')) {"error": "$input.path('$.errorMessage')"} #else {} #end -> Option A
  4. Quick Check:

    Use #if to conditionally include fields [OK]
Quick Trick: Use #if to include fields only when data exists [OK]
Common Mistakes:
MISTAKES
  • Always including error field regardless of data
  • Misusing #set without condition
  • Incorrect null checks

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More AWS Quizzes