I am attempting to escape a string in a JSP to return valid JSON on an AJAX call however the spring:escapeBody tag is not correctly escaping single quotes for JSON. Valid JSON s
As pointed out by Ryan in the (very good) comments of Sotirios Delimanolis's answer:
Any character may be escaped
. So obviously this means that even single quotes characters can be.So it seems like it's simply an implementation choice that is now leaving us with a standard that's not really consistently implemented... sigh
Anyway, here is a work around you can use to get your code working
<%@ page trimDirectiveWhitespaces="true" contentType="json/application"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%--
[1] Removing the escape of the single quote character: jQuery's Ajax cannot handle it
stackoverflow.com/questions/25491391/springescapebody-results-in-invalid-json
stackoverflow.com/questions/2275359/jquery-single-quote-in-json-response
--%>
if you don't have "user" an account
{
"status": "success",
"body" : "${fn:replace(someJsonData, "\\\'","'")}" , <%-- [1] --%>
}
Here is the JSTL fn documentation
Probably not the cleanest/best solution to be honest. But it does the job until you find better.