I have a function in a CFC file that takes a JSON string as an argument. The function then uses the deserialized data to execute an UPDATE query. In the the JSON string, one of the properties has the character #
as part of the name. This character makes the code to break in ColdFusion because it is interpreted as a variable. Is there any way to make ColdFusion "escape" that character and consider it just a string? Remember that is part of JSON string.
Below is my function. It doesn't allow me to access dnisObject
because of the #
characters within the JSON string. If I delete those #
from the JSON string it works fine. Those values have to be stored in a database with the #
, so I can not just delete them completely.
<cffunction name="updateDnisHproduct" access="remote">
<cfargument name = "lid" type = "numeric" required = "yes">
<cfargument name = "updatedObj" type = "string" required="yes">
<cfset dnisObject = DESERIALIZEJSON(arguments.updatedObj)/>
<cfset test =[{"phone":"1001106672","lineType":"Outbound","label1":"Voicemail for line #54940","label4":"test","hcat":"18","freshStart":"0","phoneCode":"","hproduct":"3","checked":false},{"phone":"1001106672","lineType":"Outbound","label1":"Voicemail Line Box #58940","label4":"12","hcat":"54","freshStart":"0","phoneCode":"","hproduct":"12","checked":false}'>
<cfset dnisObject = DESERIALIZEJSON(test)/>
</cffunction>
Same way you normally escape pound signs: double them. From the documentation:
...To include [pound signs] in a string, double the character; for example, use
##
to represent a single#
character.
Since the input is a string, just do a Replace() to substitute a single pound sign with two pound signs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With