NO MORE CAREER
POLITICIANS!
Get Out Of Our House: Replacing congress with TRUE citizens!
Contact Doug!
Learn About Doug!
View Doug Boude's online resume
updated 11/18/2009

View Doug Boude's profile on LinkedIn
Link to me!

Follow Doug Boude on Twitter
Follow me!

Be Doug's friend on Facebook
Befriend me!
(I promise not to follow you home)
OO Lexicon
Chat with Doug!
Recent Entries
You may also be interested in...
Web Hosting

best web hosting - top web hosting sites, thetop10bestwebhosting.com

Czech your Page Rank!
Check Page Rank of any web site pages instantly:
This free page rank checking tool is powered by Page Rank Checker service
Surf's Up!
Visit Egosurf.org and massage YOUR web ego!
My Score: 9,001
Doug's Books

Read (and recommend)

  • Men are from Mars, Women are from Venus
  • The Wisdom of Crowds: Why the Many Are Smarter Than the Few and How Collective Wisdom Shapes Business, Economies, Societies and Nations
  • Blink: The Power of Thinking Without Thinking
  • Head First Design Patterns
  • Transact-SQL Programming
  • What's So Amazing About Grace?
  • Just So Stories (Rudyard Kipling collection)

Reading

  • Prayer: Does it Make Any Difference?
  • Data Mining (Practical Machine Learning Tools and Techniques)
<< September, 2010 >>
SMTWTFS
1234
567891011
12131415161718
19202122232425
2627282930
Search Blog

Recent Comments
Re: Railo 3.1 on Windows Server 2008 and IIS7 - Part 3 of 3 (by Jon at 8/27 2:04 PM)
Re: Hosts File Changes Not Acknowledged on Vista 64 (by Spacy at 8/24 3:46 PM)
Re: THE DAY CFUNITED DIED (by ComboFusion at 8/23 10:50 AM)
Re: My Grandpa (by Tasha at 8/10 4:29 PM)
Re: Just What IS a 'Service Layer', Anyway? (by dougboude at 8/02 10:10 AM)
Re: Just What IS a 'Service Layer', Anyway? (by Isaac at 8/02 2:25 AM)
Re: PayPal IPN Coldfusion CFC (by Soyestudiambre at 7/25 6:12 PM)
Re: PHP vs COLDFUSION (by Tony Garcia at 7/17 11:24 AM)
Re: PHP vs COLDFUSION (by dougboude at 7/14 8:45 AM)
Re: PHP vs COLDFUSION (by Lola LB at 7/14 5:51 AM)
Categories
Archives
Photo Albums
Funnies (5)
Family (3)
RSS

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 | 8 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

Re: JSON.CFC, MAYBE CF8 DIDN'T MAKE YOU OBSOLETE AFTER ALL!
Doug - is the serializeJSON routine appreciably faster than the json encode? E.g., if it's working fine, is there any other reason to update?
Posted by Brian on October 14, 2008 at 1:04 PM

Re: JSON.CFC, MAYBE CF8 DIDN'T MAKE YOU OBSOLETE AFTER ALL!
I have a reason to use json.cfc. serializeJSON doesn't have the parameter "stringNumbers".
Posted by Warren on April 1, 2009 at 6:55 AM

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!!!

14 plus 9 equals
Type in the answer to the question you see above:

Your comment:

Sorry, no HTML allowed!