8 Replies Latest reply on Jan 15, 2014 7:19 AM by Aegis Kleais

    Why is ColdFusion adding backslashes to my Concatenated string?

    Birdwingfx Level 1

      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...

        • 1. Re: Why is ColdFusion adding backslashes to my Concatenated string?
          duncancumming Level 3

          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?
          • 2. Re: Why is ColdFusion adding backslashes to my Concatenated string?
            Birdwingfx Level 1

            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.

            • 3. Re: Why is ColdFusion adding backslashes to my Concatenated string?
              Aegis Kleais Level 3

              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.

              • 4. Re: Why is ColdFusion adding backslashes to my Concatenated string?
                Birdwingfx Level 1

                No I am not using JSStringFormat() anywhere

                 

                I ran a simple test.

                I took out the loop and set:

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

                 

                When I return that I get:

                 

                {"Success" : "true"}

                 

                 

                 

                So to further test I set:

                <cfset ActionsList = '{"Success" : "true"}, {"Test" : "test"}'>
                

                 

                When I return that i get:

                 

                "{\"Success\" : \"true\"}, {\"Test\" : \"test\"}"

                And yes I realize that is not valid JSON

                 

                So it seems like the problem is the Comma.

                 

                When there is a comma in the string Coldfusion auto-escapes the double-quotes.

                 

                Anyone have a solution for this?

                • 5. Re: Why is ColdFusion adding backslashes to my Concatenated string?
                  Birdwingfx Level 1

                  Wait... Is it possible ColdFusion is creating a Structure because of the curly brackets?

                   

                  Can I prevent coldfusion from doing this?

                  • 6. Re: Why is ColdFusion adding backslashes to my Concatenated string?
                    Aegis Kleais Level 3

                    If it was a structure, it would dump in a tabular format, or throw an error it you attempted to output the variable to screen (since it's complex)

                     

                    Let me ask you, are you using a good browser like Firefox/Chrome?  Can you inspect the request and see what the mime type of the content being rendered is?  Is this page supposed to output JSON?  What browser are you using anyways?

                    1 person found this helpful
                    • 7. Re: Why is ColdFusion adding backslashes to my Concatenated string?
                      Birdwingfx Level 1

                      Aegis Kleais wrote:

                       

                      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.

                       

                      Wow... I feel like an idiot. I still don't know why it was doing it.

                       

                      But I looked into what you said about how I was incorrectly structuring my JSON so I changed the function to:

                       <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 = Left(ActionsList, len(ActionsList)-1) & ']}'>
                          <cfreturn ActionsList>
                        </cffunction>
                      
                      

                       

                      The Left() function removes the traling comma that you talked about so that the end of my JSON would properly be "'...}]}

                       

                      I also removed the Bracket before "Actions" so the beginning is '{"success" : "true", "Actions...' Instead of '{"success" : "true"}, {"Actions" : [...'

                       

                      Once I structured my JSON properly ColdFusion removed the backslashes. The above code outputs this:

                      {"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"}]}
                      

                       

                      It seems like coldfusion adds the backslash if the JSON "String" is not created properly...

                       

                      Seems like the fault was mine and not ColdFusions.

                      • 8. Re: Why is ColdFusion adding backslashes to my Concatenated string?
                        Aegis Kleais Level 3

                        No worries. Getting a second pair of eyes on a problem has been fixing overlooked issues for ages.  It's a tried and true method!