Stringify JSON object for SQL query in AWS AppSync

前端 未结 2 1155
误落风尘
误落风尘 2021-01-27 05:11

Question: How can I stringify JSON object for SQL statement in my Appsync velocity template?

Explanation: I have an Aurora RDS table tha

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-27 05:40

    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\"",
        ]
    }
    

提交回复
热议问题