Using the array notation for creating the keys in a struct used to preserve the case of the key.
<cfset var response = structNew()>
<cfset response[ "error" ] = 0>
<cfset response[ "message" ] = "">
<!--- this worked when converting this struct to a JSON
So when I actually put some data into the struct:
<cfset response.error = 1>
<cfset response.message = "There was an error inserting the record...">
I surprised when I saw this come across the firebug console:
{"MESSAGE":"There was an error inserting the record...","ERROR":1}
note, the information is what I was expecting to see.
I understand if I would have used <cfset response.error = 0>, for the case not to be preserved when converting it to a JSON, but this has worked for me in the past. Am I doing something wrong that I’m blind to or did something change in CF10?
EDIT:
Before all of the responses came back, I tried what everyone said.
<cfset response["error"] = 1>
<cfset response["message"] = "There was an error inserting the record...">
and it worked. So, I’m guessing they changed how serializeJSON works in CF10.
Good to know.
If you continue to reference the struct key with bracket notation rather than dot notation, you should have no issue with case when using
serializeJSON().After testing on CF9, however, it seems to work fine whether I use bracket- or dot-notation. This may be a difference between CF9 and 10 and, as Ray recommends, you may want to file a bug report for it.
Here’s what I did in CF9: