-1

How to convert JSON containing JS function definitions

{  
    "a1": "5",  
    "b1": "10",  
    "c1": "function(param1,param2) { return param1 +param2}"  
}

to a JavaScript object containing those functions (and not a string with the definition):

{  
    a1: 5,  
    b1: 10,  
    c1: function(param1,param2) { return param1 + param2}  
}
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
adoweb
  • 47
  • 5
  • possible duplicate of [How to parse JSON in JavaScript](http://stackoverflow.com/questions/4935632/how-to-parse-json-in-javascript) – zzzzBov Aug 26 '13 at 13:18
  • the `"a1"` quotes do not matter, but the string for that should be replaced with `parseInt()` for that value to obtain an integer value, but the JSON string really should by `"a1":5`. Evil eval for the function? – Mark Schultheiss Aug 26 '13 at 13:18
  • 2
    **NOT a duplicate!** The guy wants the `c1` value to be evaluated. – ThiefMaster Aug 26 '13 at 13:22

4 Answers4

2

You can pass a reviver function to JSON.parse. This lets you apply custom logic to parsed values.

In this function you can test whether a the value starts with the pattern function(...) {:

var obj = JSON.parse(str, function(k, v) {

    if (/^\s*function\s*\([^)]*\)\s*{/.test(v)) {
        try {
            // using the Function constructor to evaluate the function
            // definition in global scope
            return Function('return ' + v)();
        }
        catch() {
            return v; // maybe not a JS function definition after all
        }
    }
    return v;
});

DEMO

Of course you could also just iterate over the resulting object and apply the same logic after the parsing, but using a reviver function is easier if you have nested objects/arrays.

Note that this is a very simple test and would only work for anonymous functions. If you have named functions, you have to adjust the expression accordingly. But this is the basic idea.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
0

using the JS function JSON.parse

var myobj = JSON.parse("{\"a1\": \"5\",  \"b1\": \"10\",  \"c1\": \"function(param1,param2) { return param1 +param2}\"  }");
Cybermaxs
  • 24,378
  • 8
  • 83
  • 112
0

Two step: parse the json, and promote the function source code to a function:

var jsonObj = JSON.parse(jsonFromAjax);
jsonObj.c1 = eval(jsonObj.c1);
Eric
  • 95,302
  • 53
  • 242
  • 374
-1

Here is an example that converts a json string containing a function back to a json object with a valid function declaration.

var jsonstring = "{\"schema\": {\"title\": \"User Feedback\", \"description\":\"so\", \"type\":\"object\", \"properties\":{\"name\":{\"type\":\"string\"}}}," + "\"options\":{ \"form\":{\"attributes\":{}, \"buttons\":{ \"submit\":{ \"title\":\"It\", \"click\":\"function(){alert('hello');}\" }}} }}";

var jsonData = JSON.parse(jsonstring);

function Iterate(data)
{
      jQuery.each(data, function (index, value) {
        if (typeof value == 'object') {
            Iterate(value);
        }
        else {
            if (value.indexOf("function()") > -1)
                data[index] = eval("(" + value + ")");
        }
    });

};

Iterate(jsonData);

In this case jsonData.options.form.buttons.submit.click();

Vaughn Gavin
  • 69
  • 1
  • 3