coeurdange57
coeurdange57

Reputation: 743

Escape special characters # & ' " in a string in Coldfusion

Following thie topic AngularJS - Special Characters in JSON and Coldfusion Request I have concluded that the problem didn't come from ANGULARJS or my JSON syntax. It's certainly a problem with Coldfusion.

For reminder, I'm trying to use a JSON string for doing an INSERT in a database with ColdFusion. I'm using a function defined in a component for doing that.

My function has the string as argument.

Here an example of the string used:

jsStruct={"LASTNAME":"Nämé","FIRSTN%a£öME":"TestFirstName","PHONENUMBER":48484488,"EMAIL":"[email protected]","COMPANY":"Test & Comp"}

My string is correct and the structure of the JSON is ok.

In my Coldfusion component "component.cfc":

    <cffunction name="myfunction" access="remote" returnformat="JSON" output="no">    
        <cfargument name="jsStruct" type="string" required="true">
        <cfset var cfStruct=DeserializeJSON(jsStruct)>

        ..................

    </cffunction>   

I obtain this error on the server when I use the string because in my string there is special characters. For instance "COMPANY":"Test & Comp":

JSON parsing failure: Unexpected end of JSON string


The error occurred in ../contacts.cfc: line 267

265 :       <cfargument name="jsStruct" type="string" required="true">
266 : 
267 :       <cfset var cfStruct=DeserializeJSON(jsStruct)>

Could you please help me to solve this problem and prevent (escape) special characters used by ColdFusion and Oracle as &, #, ', " and others?

Upvotes: 1

Views: 3090

Answers (1)

BKBK
BKBK

Reputation: 484

You say, in error,

My function has the string as argument. Here an example of the string used:

*jsStruct={"LASTNAME":"Nämé","FIRSTN%a£öME":"TestFirstName","PHONENUMBER":48484488,"EMAIL":"[email protected]","COMPANY":"Test & Comp"}*

You yourself call that a struct, which is correct. It is not a string.

Use something like:

jsonString='{"LASTNAME":"Nämé","FIRSTN%a£öME":"TestFirstName","PHONENUMBER":48484488,"EMAIL":"[email protected]","COMPANY":"Test & Comp"}';

Upvotes: 0

Related Questions