简体   繁体   English

如何使用 aws api 网关的速度模板转换 json 中的数组?

[英]How to transform array in json using velocity template for aws api gateway?

I've been working on writing velocity templates for aws api-gateway to transform the json response from my api to give a consistent response to the user.我一直致力于为 aws api-gateway 编写速度模板,以转换来自我的 api 的 json 响应,从而为用户提供一致的响应。

I've been trying to convert this json:我一直在尝试转换这个 json:

{
    "amountCharges": [
        "charge1", "charge2", "charge3"
    ]
}

to this expected one:对于这个预期的:

{
    "charges": [
        "charge1", "charge2", "charge3"
    ]
}

I've been able to create an array for objects using this template:我已经能够使用此模板为对象创建一个数组:

#set( $inputRoot = $input.path('$') )
"charges": [
    #foreach( $charge in $inputRoot.charges )
    {
        "type": "$charge.chargeType",
        "name": "$charge.chargeName",
        "amount": {
            "value": $charge.chargeAmount.amountValue,
            "currency": "$charge.chargeAmount.amountCurrency"
        },
        #if( $charge.chargeTax )
            "tax": {
                "name": "$charge.chargeTax.taxName",
                "amount": {
                    "value": $charge.chargeTax.taxAmount.amountValue,
                    "currency": "$charge.chargeTax.taxAmount.amountCurrency"
                }
            }
        #end
    }
    #if( $foreach.hasnext ) , #end
    #end
]

while this template has been able to convert my json object array to the desired response.虽然此模板已经能够将我的 json object 数组转换为所需的响应。 Example mentioned below:下面提到的例子:

{         
    "charges": [
    {
        "chargeType": "someType1",
        "chargeName": "someName1",
        "chargeAmount": {
            "amountValue": 5,
            "amountCurrency": "someCurrency1"
        },
        "chargeTax": {
            "taxName": "someTax1",
            "taxAmount": {
                "amountValue": 5,
                "amountCurrency": "someCurrency1"
            }
        }
    },
    {
        "chargeType": "someType2",
        "chargeName": "someName2",
        "chargeAmount": {
            "amountValue": 10,
            "amountCurrency": "someCurrency2"
        }
    }
    ]
}

the template converted this json to the expected output:该模板将此 json 转换为预期的 output:

{
    "charges": [
    {
        "type": "someType1",
        "name": "someName1",
        "amount": {
            "value": 5,
            "currency": "someCurrency1"
        },
        "tax": {
            "name": "someTax1",
            "amount": {
                "value": 5,
                "currency": "someCurrency1"
            }
        }
    },
    {
        "type": "someType2",
        "name": "someName2",
        "amount": {
            "value": 10,
            "currency": "someCurrency2"
        }
    }
    ]
}

basically, I want to convert a string array, not a key-value pair.基本上,我想转换一个字符串数组,而不是一个键值对。 Any leads would be helpful.任何线索都会有所帮助。 thanks in advance.提前致谢。

Just needed a simple tweak on my end:我只需要一个简单的调整:

#set( $inputRoot = $input.path('$') )
"charges": [
    #foreach( $amountCharge in $inputRoot.amountCharges )
        "$amountCharge"
        #if( $foreach.count < $inputRoot.amountCharges.size() ) , #end\
    #end
] 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在速度模板(AWS appsync + elasticsearch)中使用字符串替换? - How to using String replace in velocity template (AWS appsync + elasticsearch)? 如何使用 Lambda 访问 AWS API Gateway 请求的 HTTP 标头? - How to access HTTP headers for request to AWS API Gateway using Lambda? 为 API 网关和 RDS 使用 VPC | AWS - Using a VPC for API Gateway and RDS | AWS Websocket 连接使用 AWS Lambda + API 网关 - Websocket connection using AWS Lambda + API Gateway 如何使用 NLB 和 HTTP API 类型在 AWS API 网关中为不同阶段配置不同的端点? - How can I configure different endpoints for different stages using NLB and HTTP API type in AWS API Gateway? aws api 网关返回无法将负载解析为 json 无法识别的令牌 - aws api gateway return Could not parse payload into json Unrecognized token AWS API 网关签名 - AWS API Gateway Signature AWS API 网关响应正文模板映射(foreach) - AWS API gateway response body template mapping (foreach) 有没有办法使用 aws api 网关为公共 api 添加令牌? - Is there a way to add token for public api using aws api gateway? 使用 AWS API 网关和 VPC 链接时如何避免配置错误? - How to avoid the configuration error while using AWS API Gateway with VPC Link?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM