Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you return lower-cased JSON from a CFCin ColdFusion?

I have a ColdFusion component that will return some JSON data:

component
{
    remote function GetPeople() returnformat="json"
    {
        var people = entityLoad("Person");
        return people;
    }
}

Unfortunately, the returned JSON has all the property names in upper case:

[
    {
        FIRSTNAME: "John",
        LASTNAME: "Doe"
    },
    {
        FIRSTNAME: "Jane",
        LASTNAME: "Dover
    }
]

Is there any way to force the framework to return JSON so that the property names are all lower-case (maybe a custom UDF/CFC that someone else has written)?

like image 201
Daniel T. Avatar asked Sep 10 '11 00:09

Daniel T.


2 Answers

Yeah, unfortunately, that is just the way ColdFusion works. When setting some variables you can force lowercase, like with structs:

<cfset structName.varName = "test" />

Will set a the variable with uppercase names. But:

<cfset structName['varname'] = "test" />

Will force the lowercase (or camelcase depending on what you pass in).

But with the ORM stuff you are doing, I don't think you are going to be able to have any control over it. Someone correct me if I am wrong.

like image 77
Jason Dean Avatar answered Nov 19 '22 14:11

Jason Dean


From http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=functions_s_03.html
Note: ColdFusion internally represents structure key names using all-uppercase characters, and, therefore, serializes the key names to all-uppercase JSON representations. Any JavaScript that handles JSON representations of ColdFusion structures must use all-uppercase structure key names, such as CITY or STATE. You also use the all-uppercase names COLUMNS and DATA as the keys for the two arrays that represent ColdFusion queries in JSON format.

If you're defining the variables yourself, you can use bracket notation (as Jason's answer shows), but with built-in stuff like ORM I think you're stuck - unless you want to create your own struct, and clone the ORM version manually, lower-casing each of the keys, but that's not really a great solution. :/

like image 5
Peter Boughton Avatar answered Nov 19 '22 13:11

Peter Boughton