Using Javascript's Function() Constructor To Deserialize JSON Data

Posted December 10, 2009 at 2:38 PM by Ben Nadel

Tags: Javascript / DHTML

Last night, I was reading the jQuery Cookbook (review to come soon) and I came across something that I had never seen before; or rather, they mentioned something that I had never seen before (the concept was not actually demonstrated in the book). They mentioned in a side note that Javascript's Function() constructor could be used to evaluate JSON (Javascript Object Notation) data. I very rarely ever use the Function() constructor, so I wanted to see if I could get this to work.

The Function() constructor takes an optional list of argument names and final content argument and builds a function object using the given content as the method body such that this:

  • function foo( bar ){ alert( bar ); };

... and this:

  • var foo = new Function( "bar", "alert( bar );" );

... are equivalent.

Because our JSON data is string-based in its serialized format as is the above method body, we can easily create a function using the serialized JSON data:

  • <!DOCTYPE HTML>
  • <html>
  • <head>
  • <title>Using Function() To Deserialize JSON Data</title>
  • <script type="text/javascript">
  •  
  • // Mimic the JSON string as it might come back from a
  • // remote AJAX call.
  • var jsonString = "{ 'myArray': ['a','b','c'] }";
  •  
  • // Use the Function constructor to create a function
  • // that will return the JSON object. Because the Function
  • // constructor takes a string value, we can easily
  • // integrate it directly into the method definition.
  • //
  • // NOTE: Immediately invoke the method such that it
  • // returns the evaluated JSON value.
  • var jsonValue = (new Function( "return( " + jsonString + " );" ))();
  •  
  • // Log the new de-serialized JSON value.
  • console.log( jsonValue );
  •  
  • </script>
  • </head>
  • <body>
  • <!--- No data. --->
  • </body>
  • </html>

As you can see, our dynamically generated method body simply returns the JSON data. When we run the above code, we get the following Firebug console output:

 
 
 
 
 
 
Deserializing JSON Data Using Javascript's Function() Constructor. 
 
 
 

Cool - works just fine. I am not sure how this is being run internally to the Javascript engine, or if it's doing anything different than the eval() statement does. I just wanted to see if I could get it to work.




Reader Comments

Dec 10, 2009 at 4:25 PM // reply »
92 Comments

That's pretty sweet. I'd like to see some speed tests done on this code Ben. Wonder if you could just create a "deserializeJSON() method once and reuse it rather than creating a new one each time.


Dec 10, 2009 at 4:31 PM // reply »
10,640 Comments

@Andy,

As far as speed, I would assume that going the Function() route is going to be slower than just going straight to eval() simply because there's probably overhead in creating the resultant function.

I think jQuery, under the hood, will try to the native JSON object it is available, or default to eval() or something - so, the other obvious benefit to abstraction is that you can have a centralized point of branch-logic.


Dec 10, 2009 at 4:31 PM // reply »
92 Comments

Doesn't appear to work that way. Must be something special in the constructor.


Dec 10, 2009 at 4:36 PM // reply »
10,640 Comments

@Andy,

Hmmm, I don't know. I've never actually looked at their evaluation technique; but, I thought I had read something about it leveraging the internal JSON object in the new browsers.


Dec 11, 2009 at 5:08 AM // reply »
12 Comments

I feel bad about posting links from ASP.NET, but anyway this performance test is quite interesting. It's about the performance between eval(), a new function and the native support of common browsers. http://bit.ly/92Fl5


Dec 11, 2009 at 7:38 AM // reply »
10,640 Comments

@Roman,

No worries about .NET ;) It's still good results. Thanks for posting it.


Dec 11, 2009 at 10:51 AM // reply »
39 Comments

This sounds a bit sketchy. Could a hacker not use this to return a malicious script as JSON data?


Dec 11, 2009 at 10:52 AM // reply »
10,640 Comments

@Brian,

No more so than with the use of an eval() statement.


Dec 11, 2009 at 10:53 AM // reply »
39 Comments

Something else to watch for as I learn AJAX then.


Dec 11, 2009 at 10:56 AM // reply »
10,640 Comments

@Brian,

I might be very ignorant here, but I get very suspect of all the hoopla people have over the eval() / hacker stuff. It's one thing when people have an issue with AJAX-driven data; but, a lot of people get in a huff about the use of eval() to strictly internal usage.


Dec 11, 2009 at 11:04 AM // reply »
39 Comments

I'm not really sure how big a problem it is. I read about it all the time.
I follow the "best practice" guidelines whenever I find them for whatever programming languages I'm using, but is that enough to protect my apps/websites? I hope so. It's not like I'm leaving out a "Welcome Hacker" mat on my virtual front porch. But those guys can be pretty persistent in finding vulnerabilities and exploiting them.

Aside: How about posting some of YOUR Best Practices for ColdFusion or JQuery or anything really?


Dec 11, 2009 at 12:35 PM // reply »
10,640 Comments

@Brian,

MY best practices?? Hmm, that's an interesting question. I'll do some thinking and see if I can formalize some stuff.


Feb 16, 2010 at 6:36 PM // reply »
1 Comments

Thanks for this article, very helpful.


Feb 17, 2010 at 5:04 PM // reply »
10,640 Comments

@Danny,

Glad you found it useful.


Jun 20, 2011 at 12:59 PM // reply »
1 Comments

the most brilliant idea i've ever seen. i tested with ASP.NET JavaScriptSerializer class and it worked. thank you.



Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Feb 12, 2012 at 3:37 AM
Learning ColdFusion 8: CFImage Part III - Watermarks And Transparency
Hi Ben, Just to ask currently it is placed bottom right corner, if i need to replace the same rendered image on the bottom left side or in the bottom center, how that can be calculated. bottom ce ... read »
Feb 11, 2012 at 9:29 PM
Use jQuery's SlideDown() With Fixed-Width Elements To Prevent Jumping
I can't say how glad I am that I found your post. Thank you very much. ... read »
Feb 10, 2012 at 7:21 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
Update! Instead of $(eval(options.insertAfter)).after(data['insertData']); I now use: var ajaxNode = document.createElement('span'); var parent = $(eval(options.insertAfter))[0].parentNode; ... read »
Feb 10, 2012 at 6:18 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
encountered this same, what I consider, jQuery bug last week. I'm building a site in which I load some content via AJAX. This content contains Linkedin share button placeholders which Linkedin API ne ... read »
Feb 10, 2012 at 11:30 AM
Cross-Origin Resource Sharing (CORS) AJAX Requests Between jQuery And Node.js
After you understand the concepts here, this is an awesome cheatsheet for enabling CORS in just about anything http://enable-cors.org/ ... read »
JM
Feb 10, 2012 at 9:10 AM
My Safari Browser SQLite Database Hello World Example
@Amy, Here is a very good tutorial on how to use JOIN: http://www.sqltutorial.org/sqljoin-innerjoin.aspx ... read »
Feb 10, 2012 at 4:42 AM
Building A Twitter-Inspired RESTful API Architecture In ColdFusion
This is great, very useful Ben. I spotted a small typo in the api.cgm listing: <cfthrow type="Unauthroized" /> Cheers Stefan ... read »
Feb 9, 2012 at 10:35 PM
CFDirectory Filtering Uses Pipe Character For Multiple Filters (Thanks Steve Withington)
I was wondering if there would be a filter you could apply so that you got everything but what you included in the filter. As in show me all docs that are not a .pdf. ... read »