Question: How can I stringify JSON object for SQL statement in my Appsync velocity template?
Explanation: I have an Aurora RDS table tha
You can build a JSON variable like this:
#set($json = $util.toJson({
"connectionType": "$ctx.args.input.serviceConfig.connectionType",
"capacity": $ctx.args.input.serviceConfig.capacity
}))
And insert in your query:
{
"version": "2018-05-29",
"statements": [
"INSERT INTO b2b_service_catalog(service_name, service_config) VALUES ('$ctx.args.input.serviceName', '$util.escapeJavaScript($json)' RETURNING service_id AS \"serviceId\", service_name AS \"serviceName\", service_config AS \"serviceConfig\"",
]
}
The above is a bit challenging because of all the quotes and escaping but I think the use of escapeJavaScript
will do the trick.
Or directly:
{
"version": "2018-05-29",
"statements": [
"INSERT INTO b2b_service_catalog(service_name, service_config) VALUES ('$ctx.args.input.serviceName', '{\"connectionType\":\"$ctx.args.input.serviceConfig.connectionType\",\"capacity\": $ctx.args.input.serviceConfig.capacity}') RETURNING service_id AS \"serviceId\", service_name AS \"serviceName\", service_config AS \"serviceConfig\"",
]
}