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 »
11,246 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 »
11,246 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 »
11,246 Comments

@Roman,

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


Dec 11, 2009 at 10:51 AM // reply »
40 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 »
11,246 Comments

@Brian,

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


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

Something else to watch for as I learn AJAX then.


Dec 11, 2009 at 10:56 AM // reply »
11,246 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 »
40 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 »
11,246 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 »
11,246 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
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 25, 2013 at 10:01 PM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
@Avi, Really glad to help! @Jaredwilli, I'm finding a this image hits home with a lot of people :) Hopefully we can all work through the rough patches together! @Prateek, AngularJS has error ... read »
May 25, 2013 at 9:53 PM
Nested Views, Routing, And Deep Linking With AngularJS
@Mrsean2k, I'm glad I could help! I haven't been able to keep up with the ui-router stuff. I keep saying that I'll carve out time, but I just haven't gotten to it :( ... read »
May 25, 2013 at 9:49 PM
What If All User Interface (UI) Data Came In Reports?
@Jonah, Thanks for the book recommendations. I am looking them up right now. I can see that Object Thinking is available for the Kindle App - sweet! Also, I just recently heard Martin Fowler on the ... read »
May 25, 2013 at 9:41 PM
HashKeyCopier - An AngularJS Utility Class For Merging Cached And Live Data
@Chris, I'm super excited to hear that my posts are helpful. I am also loving AngularJS; but, it definitely has some caveats and some odd behaviors and some things that just don't seem to "wor ... read »
May 25, 2013 at 9:36 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
@Adam, @Jason, After reading these comments, I double-checked my latest implementation and I am happy to report that I am using listFirst() and listRest(). ... read »
May 25, 2013 at 9:31 PM
Using "//" And ".//" Expressions In XPath XML Search Directives In ColdFusion
@Daxesh, I am not sure I understand the question about the current node. If you already have a reference to the current node, why would you need to query for it? As for parent node, I believe that ... read »
May 25, 2013 at 10:08 AM
Using "//" And ".//" Expressions In XPath XML Search Directives In ColdFusion
@Ben, my question is that i want the current node with its tag and its parent node. i just want only that data. So, give me the solution for that. and remember solution is working on " xpath 1.0 ... read »
May 25, 2013 at 10:01 AM
Using "//" And ".//" Expressions In XPath XML Search Directives In ColdFusion
hey ben, i want get my current node tag and also want the root node tag withing. So, how can i fix it.. ! ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools