AWS AppSync VTL模板foreach内嵌if语句报错问题咨询
AWS AppSync VTL模板:foreach内if语句报错问题解答
问题描述
我有一个AWS AppSync VTL模板,在#foreach块内部使用#if语句时出现报错,想咨询两个问题:
- 当前使用的语法是否正确?
- 若语法不正确,该如何编写此代码?
我曾尝试使用#if($method.type == "us_bank_account")的写法,但同样无法正常运行。
当前VTL代码
#foreach($method in $body.paymentMethods) #set($transformedMethod = { "Id": $method.id, "Object": $method.object, "BillingDetails": { "Email": $method.billing_details.email, "Name": $method.billing_details.name, "Phone": $method.billing_details.phone }, "Created": $method.created, "Customer": $method.customer, "Livemode": $method.livemode, "Metadata": $utils.toJson($method.metadata), "Type": $method.type, #if($method.type.equals("us_bank_account")) "USBankAccount": { "AccountHolderType": $method.us_bank_account.account_holder_type, "AccountType": $method.us_bank_account.account_type, "BankName": $method.us_bank_account.bank_name, "FinancialConnectionsAccount": $method.us_bank_account.financial_connections_account, "Fingerprint": $method.us_bank_account.fingerprint, "Last4": $method.us_bank_account.last4, "Networks": { "Preferred": $method.us_bank_account.networks.preferred, "Supported": $utils.toJson($method.us_bank_account.networks.supported) }, "RoutingNumber": $method.us_bank_account.routing_number, "StatusDetails": $method.us_bank_account.status_details } #end }) $util.qr($paymentMethods.add($transformedMethod)) #end
源JSON数据
{ "isSuccess": true, "paymentMethods": [ { "billing_details": { "address": { "city": null, "country": null, "line1": null, "line2": null, "postal_code": null, "state": null }, "email": null, "name": "Test Account Holder", "phone": null }, "created": 1701975288, "customer": "cus_P60pU8eyi7OM1T", "id": "pm_1OKmiiApDBfa1h4dHUrVeVhm", "livemode": false, "metadata": {}, "object": "payment_method", "type": "us_bank_account", "us_bank_account": { "account_holder_type": "individual", "account_type": "checking", "bank_name": "STRIPE TEST BANK", "financial_connections_account": null, "fingerprint": "vFaCL0w3N7j2qWhO", "last4": "6789", "networks": { "preferred": "ach", "supported": [ "ach" ] }, "routing_number": "110000000", "status_details": {} } }, { "billing_details": { "address": { "city": "Libertyville", "country": "US", "line1": "1401 Franklin Blvd.", "line2": null, "postal_code": "60048", "state": "IL" }, "email": null, "name": "Test", "phone": null }, "card": { "brand": "visa", "checks": { "address_line1_check": "pass", "address_postal_code_check": "pass", "cvc_check": null }, "country": "US", "exp_month": 12, "exp_year": 2027, "fingerprint": "cNo8pU2BD7OMVNAa", "funding": "credit", "generated_from": null, "last4": "4242", "networks": { "available": [ "visa" ], "preferred": null }, "three_d_secure_usage": { "supported": true }, "wallet": null }, "created": 1701974966, "customer": "cus_P60pU8eyi7OM1T", "id": "card_1OKmdWApDBfa1h4d0WRWWO7W", "livemode": false, "metadata": {}, "object": "payment_method", "type": "card" } ] }
问题解答
1. 当前语法是否正确?
当前语法存在两个核心问题:
- JSON结构语法错误:
#if块前后的键值对会引发逗号问题——条件不满足时,"Type": $method.type,末尾的逗号会导致JSON格式无效;条件满足时,USBankAccount块末尾无后续键值对也会触发语法错误。 - 空值处理缺失:若
$method.type为null,调用.equals()方法会直接抛出空指针异常;使用==比较字符串虽可行,但同样未处理空值场景。
2. 修正后的代码写法
通过拆分对象构建逻辑、增加空值判断来解决问题:
#foreach($method in $body.paymentMethods) #set($transformedMethod = { "Id": $method.id, "Object": $method.object, "BillingDetails": { "Email": $method.billing_details.email, "Name": $method.billing_details.name, "Phone": $method.billing_details.phone }, "Created": $method.created, "Customer": $method.customer, "Livemode": $method.livemode, "Metadata": $utils.toJson($method.metadata), "Type": $method.type }) #if($method.type && $method.type == "us_bank_account") #set($void = $transformedMethod.put("USBankAccount", { "AccountHolderType": $method.us_bank_account.account_holder_type, "AccountType": $method.us_bank_account.account_type, "BankName": $method.us_bank_account.bank_name, "FinancialConnectionsAccount": $method.us_bank_account.financial_connections_account, "Fingerprint": $method.us_bank_account.fingerprint, "Last4": $method.us_bank_account.last4, "Networks": { "Preferred": $method.us_bank_account.networks.preferred, "Supported": $utils.toJson($method.us_bank_account.networks.supported) }, "RoutingNumber": $method.us_bank_account.routing_number, "StatusDetails": $method.us_bank_account.status_details })) #end $util.qr($paymentMethods.add($transformedMethod)) #end
关键修正点说明
- 拆分对象构建:先创建无条件属性的基础对象,再通过
$transformedMethod.put()动态添加USBankAccount属性,彻底规避JSON逗号语法错误。 - 空值安全判断:
#if($method.type && $method.type == "us_bank_account")先检查$method.type是否存在,再做字符串比较,防止空指针异常。 - 屏蔽返回值:
$transformedMethod.put()会返回旧值,用#set($void = ...)避免输出不必要的内容。
内容的提问来源于stack exchange,提问作者Denil Parmar
相关产品推荐
相关产品推荐

