Hello AWS Community,
I encountered an issue with my AWS API Gateway response mapping template when using a direct connection to a DynamoDB Scan function. The response mapping template is used to structure the JSON response returned from the Scan operation.
Problem:
When the DynamoDB Scan function returns exactly two items, the response mapping template adds an extra comma after the second item in the JSON response, resulting in an invalid JSON format. This issue does not occur when the Scan function returns three or more items.
JSON Response with the Extra Comma:
{
"Data": [
{
"factura": "WB-90225",
"num_serie": "1l7xko20",
"c_almacen": "101",
"c_producto": "12299",
"cc_cliente": "66775966",
"d_almacen": "BODEGA WEBSERVICE",
"d_categoria": "SUEROS Y TRATAMIENTOS",
"d_linea": "CUIDADO DE PIEL",
"d_marca": "THE ORDINARY",
"d_producto": "TO NIACINAMIDE 10 ZINC 1 - 30ML",
"d_proveedor": "",
"d_sector": "IMPORTADO",
"d_tipo_venta": "CONTADO",
"f_factura": "01/01/2023",
"nombre_completo": "Carolina Uribe",
"st_mode_transacction": "En linea (R)",
"TimeToLive": 1690417034,
"precio_venta_un": "44000.00",
"vr_descuento": "0",
"vr_iva": "7025",
"vr_neto": "44000"
},
{
"factura": "WB-90225",
"num_serie": "9pg5ho3q",
"c_almacen": "101",
"c_producto": "9232",
"cc_cliente": "66775966",
"d_almacen": "BODEGA WEBSERVICE",
"d_categoria": "SUEROS Y TRATAMIENTOS",
"d_linea": "CUIDADO DE PIEL",
"d_marca": "GOOD MOLECULES",
"d_producto": "GM HYALURONIC ACID SERUM ML",
"d_proveedor": "",
"d_sector": "IMPORTADO",
"d_tipo_venta": "CONTADO",
"f_factura": "01/01/2023",
"nombre_completo": "Carolina Uribe",
"st_mode_transacction": "En linea (R)",
"TimeToLive": 1690417026,
"precio_venta_un": "47000.00",
"vr_descuento": "0",
"vr_iva": "7504",
"vr_neto": "47000"
},
],
"LastEvaluatedKey": {
"factura": "WB-1790",
.....
As you can see from the JSON response above, the extra comma appears after the second item when the response contains exactly two items.
Response Mapping Template:
#set($isEmpty = $input.path('$.Items').isEmpty())
#set($lastEvaluatedKey = $input.path('$.LastEvaluatedKey'))
#if($isEmpty)
#set($context.responseOverride.status = 404)
{
"message": "No hay resultados"
}
#else
#set($items = $input.path('$.Items'))
#set($lastEvaluatedKeyfactura = $lastEvaluatedKey.factura.S)
#set($lastEvaluatedKeynum_serie = $lastEvaluatedKey.num_serie.S)
{
"Data": [
#foreach($item in $items)
#set($d_producto = $item.d_producto.S)
#if($d_producto != "EC TRANSPORTE")
#set($factura = $item.factura.S)
#set($c_almacen = $item.c_almacen.S)
#set($c_producto = $item.c_producto.S)
#set($cc_cliente = $item.cc_cliente.S)
#set($d_almacen = $item.d_almacen.S)
#set($d_categoria = $item.d_categoria.S)
#set($d_linea = $item.d_linea.S)
#set($d_marca = $item.d_marca.S)
#set($d_proveedor = $item.d_proveedor.S)
#set($d_sector = $item.d_sector.S)
#set($d_tipo_venta = $item.d_tipo_venta.S)
#set($f_factura = $item.f_factura.S)
#set($nombre_completo = $item.nombre_completo.S)
#set($st_mode_transacction = $item.st_mode_transacction.S)
#set($TimeToLive = $item.TimeToLive.N)
#set($precio_venta_un = $item.precio_venta_un.S)
#set($vr_descuento = $item.vr_descuento.S)
#set($vr_iva = $item.vr_iva.S)
#set($vr_neto = $item.vr_neto.S)
{
"factura": "$factura",
"c_almacen": "$c_almacen",
"c_producto": "$c_producto",
"cc_cliente": "$cc_cliente",
"d_almacen": "$d_almacen",
"d_categoria": "$d_categoria",
"d_linea": "$d_linea",
"d_marca": "$d_marca",
"d_producto": "$d_producto",
"d_proveedor": "$d_proveedor",
"d_sector": "$d_sector",
"d_tipo_venta": "$d_tipo_venta",
"f_factura": "$f_factura",
"nombre_completo": "$nombre_completo",
"st_mode_transacction": "$st_mode_transacction",
"TimeToLive": $TimeToLive,
"precio_venta_un": $precio_venta_un,
"vr_descuento": $vr_descuento,
"vr_iva": $vr_iva,
"vr_neto": $vr_neto
}#if(!$foreach.last),#end
#end
#end
],
"LastEvaluatedKey": {
"factura": "$lastEvaluatedKeyfactura",
}
}
#end
Thanks in advance for your time
Thanks. I did it but the problem remains. It is strange that this happens only when the response has 2 items. For 3 or more this final comma just removes automatically.