Search code examples
aws-api-gatewayapi-gatewayvtlvelocity-template-language

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.

I've been trying to convert this 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. 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:

{
    "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.


Solution

  • 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
    ]