Run json string function

I have a json line:

json = "{'run': 'function() { console.log('running...'); }'}" 

How to run this function inside json string?

+4
source share
6 answers

You will need to use the eval() ( doc ) function. Many people have a lot of feelings about this feature. JSON is best suited for transporting data, not functions (see JSON ). Functions must be in the script on the page. There is also a syntax error in your published code (the function is wrapped in single quotes ('), as well as the first parameter to console.log). But...

 json = "{\"run\":\"function() { console.log('running...'); }\"}"; //Fixed, thanks obj = JSON.parse(json); eval(obj.run); //Logs "running..." 

Update:

Oh, and I was wrong. Eval does not seem to like anonymous functions. With the redesigned code, it will parse json into an object with the run property, which is a String , with the value "function() { console.log('running...'); }" . But when do you eval(obj.run); , you will receive a SyntaxError message about the unexpected (presumably this (in function ( ).

So, I can think of two ways to deal with this:

  • Remove the anonymous function in your real JSON string (so make your PHP forget about function () { ) and evaluate it. This means that it will be called as soon as you eval it.
  • I think you want it to be evaluated by an anonymous function that will be called whenever you want. Thus, you can write a wrapper function (for this you will also need to choose option 1):

     function returnEval(str) { return function () { eval(str); } } 

    This will allow you to call it. So:

     obj = JSON.parse(json); obj.run = returnEval(obj.run); obj.run(); //Logs "running..." 

Hope this helps!

+7
source

JSON is not really intended for this, but seems like a good example here.

0
source

This works for me in Firefox:

 var json = "{'run': 'function() { console.log(\\'running...\\'); }'}"; eval('var j = ' + json); eval('var r = ' + j.run); r(); 
0
source

Try it, it works:

 var JS = { "function" : "alert( new Date().getTime() );" }; new Function ( "", JS["function"] )(); 

for nested functions, you can also use something like this:

 jQuery.each( JS, function( method, value ) { new Function ( "a, b", "if ( a == 'function' ) { new Function ( '', b )(); }" )( method, value ); } ); 
0
source

I know the stream is old, but I want to share with you guys. You can make a json string and parse easily with these functions.

 function makeString(val){ return JSON.stringify(val, function (key, value) {if (typeof value == 'function') {return value.toString();}return value;}); } function parseIt(val){ return JSON.parse(string, function (key, value) {if (value.toString().search("function")>-1) {eval("var func = " + value);return func;}return value;}); } 
0
source

Without using eval('function()') you can create a new function using new Function(strName) . The following code has been tested using FF, Chrome, IE.

 <html> <body> <button onclick="test()">Try it</button> </body> </html> <script type="text/javascript"> function test() { try { var myJSONObject = {"success" : true, "jsFunc" : "myFunction()"}; var fnName = myJSONObject.jsFunc; var fn = new Function(fnName); fn(); } catch (err) { console.log("error:"+err.message); } } function myFunction() { console.log('Executing myFunction()'); } </script> 
0
source

All Articles