Oh my god. It's full of code!

Coldfusion serializeJson preserve case

Hey this is just a quick tip for any of you ColdFusion programmers who may be interacting with javascript while using JSON. ColdFusion being a non case sensative language has an annoying habit of changing the case of your structure key names when being fed through the serializeJson function, or most other functions that translate ColdFusion datastructures. The official statment is that you just need to change your javascript to expect all uppercase key names, but in some cases you cannot do that. There is a workaround however. Instead of declairing your structure keys like

<cfset myStruct = structnew()>
<cfset myStruct.keyName = "I won't preserve this key's case!">
<!---- this will be all uppercase. Sad face 😦 ----->
<cfoutput>#serializeJson(myStruct)#</cfoutput>

use

<cfset myStruct = structnew()>
<cfset myStruct['keyName'] = "I WILL preserve this key's case!">
<!---- this will preserve case. yay! ----->
<cfoutput>#serializeJson(myStruct)#</cfoutput>

so even after sending that structure through serializeJson the case of key name in the second example is preserved. Hope this helps someone, cause I know it was driving me crazy for a while!

4 responses

  1. Pingback: Coldfusion serializeJson preserve case « I Write Crappy Code » WB Tips

  2. Dane

    Thanks a lot for this one – it helped me out!

    April 6, 2011 at 4:12 pm

  3. Brad P.

    Thanks for this post, exactly what I was looking for.

    September 22, 2015 at 12:07 am

  4. Anonymous

    Awesome!

    May 3, 2016 at 3:58 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s