Converting structkeys to lowercase

As you most probably know when you create a structure and don't quote the keys they are automatically converted to uppercase.


<cfset myStruct = structNew()>
<cfset mystruct.one=1>
<cfset mystruct.two=2>
<cfset mystruct.three=3>

When you dump it out you get:

and when you serialize it to json all the keys are also uppercase. This can be problematic since JS is case sensitive. You could create your structure with lowercase keys then serializeJSON() will maintain the case of your structure, but if you don't have control of the structure itself you need to do something to to convert only the structure keys to lowercase.

As a quick and dirty way of doing this I build a recursive function to do just that. Firstly I ran the structure through SerializeJSON() just in case the structure contained other datatypes that also need to be serialized, I think deserialized the string back into a structure. Make sure you use strict mapping so that the function doesn't try to convert your query back into a query. Then call the recursive function to go through and change all the keys to lowercase.

Here is the function:


    <cffunction name="convertStructToLower" access="public" returntype="struct">
        <cfargument name="st" required="true" type="struct">

        <cfset var aKeys = structKeyArray(st)>
        <cfset var stN = structNew()>
        <cfset var i= 0>
        <cfset var ai= 0>
        <cfloop array="#aKeys#" index="i">
            <cfif isStruct(st[i])>
                <cfset stN['#lCase(i)#'] = convertStructToLower(st[i])>
            <cfelseif isArray(st[i])>
                <cfloop from=1 to="#arraylen(st[i])#" index="ai">
                    <cfif isStruct(st[i][ai])>
                        <cfset st[i][ai] = convertStructToLower(st[i][ai])>
                    <cfelse>
                        <cfset st[i][ai] = st[i][ai]>
                    </cfif>
                </cfloop>
                <cfset stN['#lcase(i)#'] = st[i]>
            <cfelse>
                <cfset stN['#lcase(i)#'] = st[i]>
            </cfif>
        </cfloop>
        <cfreturn stn>
    </cffunction>

As you can see it also handles the case where you have an array of structures inside a structure.

I've tested it with nested structures that contain arrays of structures and it seems to work just fine. Perhaps you will find it useful for any Ajax stuff you are doing.

Happy Coding...

4 Comments to "Converting structkeys to lowercase"- Add Yours
John Allen's Gravatar Useful. Thanks.
# Posted By John Allen | 7/14/10 8:13 AM
Mae's Gravatar Nice tips! Thank you this could be very useful to us.
# Posted By Mae | 8/11/10 7:08 PM
Pavan's Gravatar One other way to accomplish that is by not using the dot notation.
So instead of using mystruct.one = 1, use mystruct["one"] = 1. Dumping that, you should have the structure keys in lowercase.
# Posted By Pavan | 10/19/10 3:51 PM
patrick's Gravatar Just what I needed, thanks gary/google
# Posted By patrick | 10/14/13 2:25 AM

Powered By Railo

Subscribe

Subscribe via RSS
Follow garyrgilbert on Twitter Follow me on Twitter
Or, Receive daily updates via email.

Tags

adobe air ajax apple cf community cfml coldfusion examples ext flash flex google javascript jquery max2007 max2008 misc open source programming railo software technology ui

Recent Entries

No recent entries.

Blogroll

An Architect's View
CFSilence
Rey Bango
TalkingTree

Wish List

My Amazon.com Wish List