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\"",
]
}
Solution 1:
Thanks to @cyberwombat I was able to fix this issue. I am posting this as a reference if someone needs it.
My serviceConfig
object is not fixed and may change with time so I modified my resolver to make it more generic and it looks something like this:
#set($serviceConfigMap = {})
#foreach($key in $ctx.args.input.serviceConfig.keySet())
$util.qr($serviceConfigMap.put("$key", $ctx.args.input.serviceConfig.get($key)))
#end
#set($serviceConfigMap = $util.toJson($serviceConfigMap))
{
"version": "2018-05-29",
"statements": [
"INSERT INTO b2b_service_catalog(service_name, service_config) VALUES ('$ctx.args.input.serviceName', '$util.escapeJavaScript($serviceConfigMap)') RETURNING service_id AS \"serviceId\", service_name AS \"serviceName\", service_config AS \"serviceConfig\"",
]
}
Old Solution:
Here's how I solved this previously.
I converted my resolver to Pipeline and created a simple Lambda function in NodeJS to stringify the JSON object. This is how my lambda function looks like:
exports.handler = (event, context, callback) => {
const response = {
statusCode: 200,
body: JSON.stringify(event.input.serviceConfig).replace(/\"/g, '\\\"'),
};
callback(null, response)
};
This is how serviceConfig looks like now and exactly how I wanted:
{\"connectionType\":\"ftth\",\"maxUploadCapacity\":1}"
I can now easily use it in my SQL statement within my resolver.