I recently needed to connect Squiz Matrix to a REST API using a POST request.
The process of connecting the form to API is fairly simple, but I was stumped at how to handle quotes inside the form submission keywords.
I had created a JSON format POST, e.g.
{ "First_Name" : "%response_12345_q1%", "Last_Name" : "%response_12345_q2%", }
But this would create broken JSON if the form user entered a quote into the form field.
The solution to this is ensuring the input is escaped – so the quotes would have a backslash before them – \”
But how do you do this with Squiz Matrix?
Fortunately they have keyword modifiers. They offer a surprisingly long list of modifiers that allow you to customise the content from a keyword – most importantly is ^escapequotes.
To use, simply add to the end of your content keyword, for example:
{ "First_Name" : "%response_12345_q1^escapequotes%", "Last_Name" : "%response_12345_q2^escapequotes%", }
Which will automatically add the required backslashes to any double quotes – e.g. Jon “Bob” would become Jon \”Bob\”.