Doug's Resume
OO Lexicon
Chat with Doug!
Recent Entries
You may also be interested in...

heaters
hotels boeken in 7 sec
Engagement Rings
Online Dating Australia




SURF'S UP!
You:
Your Web Site:
<< May, 2008 >>
SMTWTFS
123
45678910
11121314151617
18192021222324
25262728293031
Search Blog

ColdFusion Jobs
Recent Comments
Re: The Perfect Alternative to Gas Powered Vehicles (by Thomas Messier at 5/09 12:47 PM)
Re: Promoting Family Unity: Lowering Your Utility Bills! (by Fernando Lopez at 5/07 10:12 PM)
Re: Why I Hate ORMs (a solicited rant) (by Richard at 5/06 10:56 AM)
Re: Why I Hate ORMs (a solicited rant) (by dougboude at 5/06 10:27 AM)
Re: Why I Hate ORMs (a solicited rant) (by Richard at 5/06 6:50 AM)
Re: Why I Hate ORMs (a solicited rant) (by Sean Corfield at 5/06 1:40 AM)
Re: Why I Hate ORMs (a solicited rant) (by Steve Bryant at 5/05 5:07 PM)
Re: Why I Hate ORMs (a solicited rant) (by dougboude at 5/05 4:36 PM)
Re: Why I Hate ORMs (a solicited rant) (by Mark Mandel at 5/05 3:52 PM)
Re: Why I Hate ORMs (a solicited rant) (by dougboude at 5/05 3:42 PM)
Categories
Archives
Photo Albums
Funnies (5)
Family (3)
RSS
Reciprocal Links

Powered by
BlogCFM v1.11

06 February 2008
JSON.CFC, MAYBE CF8 DIDN'T MAKE YOU OBSOLETE AFTER ALL!

Back in July I shared the results of my personal forray into JSON as a blog post, comparing it to XML as a means of returning Ajax data. In order to conduct my experiments, I had utilized an open source CFC called JSON.CFC in order to do the conversion from CF data types to JSON and vica versa. After the post, one individual made the comment that once CF 8 came out, there wouldn't be a need for JSON.CFC anymore. Well, until today, I would have agreed, but now I'm not so sure.

I'm working on a project where I want to retrieve some data sets and keep them client side for fast lookups, so my approach was to return the data to my template as a JSON string, and then just 'pop' it into Javascript, like so:

<script>
 var seriesgroups = <cfoutput>#seriesgroups#</cfoutput>;
</script>

FYI, the my data has these fields:  SERIESGROUPID,NAME

What I expected was that, once I did this, I would be able to access my values in JS like this:

<input type="button" value="test" onClick="alert(seriesgroups.data.name[0]);" />

...


My data was converted from a query to JSON using CF8's new "serializeJSON()" function. But to my mild irritation, I was not able to access the data as expected. I thought to myself, "self, this is an odd thing, because when I performed the same task using the JSON component a few months ago, it worked fine". Just to double check myself, I re-read my blog post on JSON and decided to use JSON.CFC to see if the results were the same. Using JSON.CFC, I was able to get the expected results.

So here's the thing, then: JSON.CFC, MAYBE YOU'RE NOT SO OBSOLETE AFTER ALL!

Or, it could also be that I am just not adept enough at reading JSON strings and can't figure out the appropriate way to access my JS object in such a way that I could easily loop over it looking for specific values. If anyone knows how one SHOULD go about accessing data that was serialized using "serializeJSON" in a JS context, I would love to see an example! I could probably figure it out if I had the luxury of time, but alas, I do not.

Okay, just to top things off with a side by side comparison (which I love doing), here are the results of the CF 8 and JSON.CFC conversions:

Method JSON String
JSON.CFC {"recordcount":1,"columnlist":"name,seriesgroupid","data":{"name":["Exports"],"seriesgroupid":[1]}}
serializeJSON() {"COLUMNS":["SERIESGROUPID","NAME"],"DATA":[[1,"Exports"]]}


JSON.CFC dump serializeJSON dump
dump of cfjson results dump of the serializeJSON results



Posted by dougboude at 4:20 PM | PRINT THIS POST! |Link | 6 comments
Subscription Options

You are not logged in, so your subscription status for this entry is unknown. You can login or register here.

Re: JSON.CFC, MAYBE CF8 DIDN'T MAKE YOU OBSOLETE AFTER ALL!
You may want to look at the serializeQueryByColumns argument of the SerializeJSON function. Setting it to true I think it does what you are looking for.
Posted by Sam Farmer on February 6, 2008 at 5:27 PM

Re: JSON.CFC, MAYBE CF8 DIDN'T MAKE YOU OBSOLETE AFTER ALL!
Um, you do know that serializeJSON has a serializeQueryByColumns attribute? Did you try that option?
Posted by Raymond Camden on February 6, 2008 at 6:01 PM

Re: JSON.CFC, MAYBE CF8 DIDN'T MAKE YOU OBSOLETE AFTER ALL!
You will also need to be careful with case sensitivity in JavaScript. ColdFusion converts everything to upper case, which can be slightly annoying... :\
Posted by Justin Carter on February 6, 2008 at 10:02 PM

Re: JSON.CFC, MAYBE CF8 DIDN'T MAKE YOU OBSOLETE AFTER ALL!
Justin is right, but you can get around that by using associative array notation if you're rolling your own structure.

Eg: <cfset struct["foo"] = value />
Posted by todd sharp on February 7, 2008 at 11:08 AM

Re: JSON.CFC, MAYBE CF8 DIDN'T MAKE YOU OBSOLETE AFTER ALL!
Nope, didn't know about that attribute, Ray (otherwise I would've given it a whirl). Thanks for the input, though! I knew that the wisdom of the crowd wouldn't fail me! :)
Posted by dougboude on February 7, 2008 at 11:31 AM

Re: JSON.CFC, MAYBE CF8 DIDN'T MAKE YOU OBSOLETE AFTER ALL!
Implemented the serializeQueryByColumns attribute, and it worked like a charm. I can now access my js object as I did when using JSON.CFC. I guess that means you are obsolete, JSON.CFC. :( Sorry!
Posted by dougboude on February 7, 2008 at 12:36 PM

Name:   Required
Email:   Required your email address will not be publicly displayed.

Want to receive notifications when new comments are added? Login/Register for an account.

Time to take the Turing Test!!!

What letter comes two place(s) after the letter L?
Type your answer exactly two time(s) in the designated box.

Type in the answer to the question you see above:

Your comment:

Sorry, no HTML allowed!