39

Is there any way that I can pass a function as a json string (conversion with JSON.stringify), send it to another function, parse the json and then execute the function that was in the json? I am using jquery and javascript.

amateur
  • 43,371
  • 65
  • 192
  • 320

7 Answers7

62

Yes, you can. There are tons of ways to do it.

And there is no need to use the "evil" eval function (please yahoogle why it should be avoided) as pointed out here: http://javascript.about.com/library/bleval.htm

var tmpFunc = new Function(codeToRun);
tmpFunc(); 

Whether it was JSON at any stage should be irrelevant.

EasierSaidThanDone
  • 1,877
  • 4
  • 20
  • 29
22

Yes, you can convert a function to a string with it's toString() method.

Here's an example to show converting a function to a string and back to a function:

var myfunc = function () {
    alert('It works!');
}

var as_string = myfunc.toString();

as_string = as_string.replace('It works', 'It really works');

var as_func = eval('(' + as_string + ')');

as_func();
Skilldrick
  • 69,215
  • 34
  • 177
  • 229
20

Here's a working example

Basically, you have to be careful with this sort of thing. If you take an extant javascript function, turn it to a string, and eval it, you might run into function redeclaration issues. If you are simply taking a function string from the server and you want to run it, you can do as I did on that jsfiddle:

Javascript

var myFunc = "function test() {alert('test');}";

$(document).ready(function() {
    var data = new Object();
    data.func = myFunc;
    var jsonVal = $.toJSON(data);
    var newObj = $.evalJSON(jsonVal);
    eval(newObj.func);
    test();
});​
Rahil Wazir
  • 10,007
  • 11
  • 42
  • 64
treeface
  • 13,270
  • 4
  • 51
  • 57
5

take a look at the JSONfn plugin.

http://www.eslinstructor.net/jsonfn/

it does exactly what you're asking for.

-Vadim

vadimk
  • 1,461
  • 15
  • 11
3

I created a fork of JSONfn which enables you to stringify and parse objects and their prototypes. In my basic tests it worked fine.

https://github.com/cgarciae/jsonfn

Cristian Garcia
  • 9,630
  • 6
  • 54
  • 75
0

I've found it helpful to use the JavaScript call() function when working with functions in JSON files.

var returnData = theJsonData.theFunction.call();
console.log(returnData); // prints any return data

I hope that helps anyone that stops by!

Bennybear
  • 335
  • 2
  • 13
-11

No, you cannot do this. Functions cannot be JSON serialized. Instead of converting the object into JSON you could directly pass it to the other function without calling JSON.stringify.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 24
    JSON = JavaScript Object Notation - of course the function can be serialized and passed to and fro. What is an object ? Pretty much anything in JS, functions included. Get a bit annoyed when someone says "No" when "YES" is the proper answer. Better to say 'not that I am aware of'. Bill –  Mar 09 '11 at 10:48
  • 1
    I'd think ten times before saying that smt is not possible in JavaScript :) Hardly can recall even a few "NO"s in JS per se. "HOW" and "WHY NOT" are the most common questions. – Arman Jun 15 '14 at 16:42
  • 2
    @ArmanMcHitarian You forgot the third-most-common question: "WHY IN THE WORLD DOES IT DO THAT!?" – Moshe Katz Dec 21 '15 at 23:00
  • As others have pointed out this can be done. – theking2 May 08 '21 at 15:59