I have grails app that makes a REST call. If an error occurs, a JSON array containing the error messages is returned. I need to combine these strings into a single string. However, when I do so double quotes are added to the front and end of the string. I have written a simple test controller to illustrate the problem:
import net.sf.json.*
class MyController {
def test = {
String msg = "'fred' is not a valid LDAP distinguished name."
JSONArray messages = new JSONArray()
messages.add(msg)
def renderStr = messages.join('<br/>')
render(renderStr)
}
}
The output looks like this:
"'fred' is not a valid LDAP distinguished name."
The problem is that the join function returns JSON-spec strings… which, according to their documentation here: http://grails.org/doc/1.0.3/api/org/codehaus/groovy/grails/web/json/JSONArray.html
Note that the rule that “Strings do not need to be quoted at all if they do not begin with a quote or single quote” is what’s happening. Your string starts with a quote, so if it was output without quotes, a JSON parser would assume the string ended at the 2nd single-quote, and the text afterwards would be unparseable garbage.