Skip to main content
Participant
January 13, 2014
Answered

Why is ColdFusion adding backslashes to my Concatenated string?

  • January 13, 2014
  • 1 reply
  • 5237 views

Alright, serializeJSON isn't creating pretty enough JSON for me to use with jQuery.

So I decided to build it myself.

Here is my code:

<cffunction name="getActions" output="false" access="remote" returnformat="JSON">

    <cfquery name="Actions">

      SELECT * FROM Actions

    </cfquery>

    <cfset ActionsList = '{"success" : "true"}, {"Actions" : ['>

    <cfloop query="Actions">

      <cfset ActionsList = ActionsList & '{ "ID" : "' & ID & '", "Name" : "' & Name & '"},'>

    </cfloop>

    <cfset ActionsList = ActionsList & ']}'>

    <cfreturn ActionsList>

  </cffunction>

So basically I send an AJAX Asynchronous call with jQuery to the cfc that contains this code.

jQuery kept telling me the JSON was bad.

So I output what Coldfusion was sending back to the browser.

"{\"success\":\"true\"},{\"Actions\":[{\"ID\":\"1\",\"Name\":\"View Public\"},{\"ID\":\"2\",\"Name\":\"View Full\"},{\"ID\":\"3\",\"Name\":\"Create Page\"},

{\"ID\":\"4\",\"Name\":\"Create Part\"},{\"ID\":\"5\",\"Name\":\"Create Section\"},{\"ID\":\"6\",\"Name\":\"Create Question\"},{\"ID\":\"7\",\"Name\":\"Create Table\"},

{\"ID\":\"8\",\"Name\":\"Assign Pages to Location\"},{\"ID\":\"9\",\"Name\":\"Input Data\"},{\"ID\":\"10\",\"Name\":\"Edit Page\"},{\"ID\":\"11\",\"Name\":\"Edit Part\"},

{\"ID\":\"12\",\"Name\":\"Edit Section\"},{\"ID\":\"13\",\"Name\":\"Edit Question\"},{\"ID\":\"14\",\"Name\":\"Edit Table\"},

{\"ID\":\"15\",\"Name\":\"Assign Pages to Location Type\"},{\"ID\":\"16\",\"Name\":\"Add Location\"},{\"ID\":\"17\",\"Name\":\"Add Location Type\"},

{\"ID\":\"18\",\"Name\":\"Change Location\"},{\"ID\":\"19\",\"Name\":\"Assign Location Designee\"},{\"ID\":\"20\",\"Name\":\"Assign District Designee\"},

{\"ID\":\"21\",\"Name\":\"Update User Information\"},{\"ID\":\"22\",\"Name\":\"Add User Type\"},{\"ID\":\"23\",\"Name\":\"Manage Permissions\"},]}\""

Why is coldfusion adding a backslash before every double quote?

I built JSON the sam exact way in another function and it worked fine.

The only difference between that function and this one is this one has a loop.

I have been at this for hours... what am I missing?

and replace(ActionsList,"\","","ALL")

doesn't help...

    This topic has been closed for replies.
    Correct answer Aegis_Kleais

    You're not performing a JSStringFormat() on that anywhere, are you?  (maybe in some code not posted).  JSStringFormat() assumes the text passed to it is a literal string, and then it will escape quoted values (like we're seeing here)

    Also, on an aside, your JSON data is invalid, no?

    Your loop updates the action list with an array, but it always assumes there is another index of values, so it prepends the comma.  You can even see in the final version your array of data ends with a '...},]}'  You may have to do a conditional test to see whether the loop is at it's max index, and if not, then prepend the comma.

    1 reply

    Participating Frequently
    January 14, 2014

    The string you're outputting is wrapped in double-quotes.  So the double-quotes inside that string need to be escaped, which is what the backslashes are doing.

    You say you have another function doing this that works fine; what's the difference? 

    • Different returntype or access attributes? 
    • Are you using single-quotes instead of double-quotes in that function? 
    • Are you outputting it differently?
    Participant
    January 14, 2014

    Thank you for the reply. Both functions are exactly the same exept for the loop.

    If you look closely, the string in ColdFusion is actualy wrapped in single-quotes not double-quotes, but when it gets output coldfusion is adding double-quotes instead.

    Perhaps it's doing it because this JSON string has an array in it?

    I will remove the array and pass just the success element and see if that does anything.

    Aegis_KleaisCorrect answer
    Inspiring
    January 14, 2014

    You're not performing a JSStringFormat() on that anywhere, are you?  (maybe in some code not posted).  JSStringFormat() assumes the text passed to it is a literal string, and then it will escape quoted values (like we're seeing here)

    Also, on an aside, your JSON data is invalid, no?

    Your loop updates the action list with an array, but it always assumes there is another index of values, so it prepends the comma.  You can even see in the final version your array of data ends with a '...},]}'  You may have to do a conditional test to see whether the loop is at it's max index, and if not, then prepend the comma.