4

I have this JSON as a result of the function JSON.stringify():

{"get":"function (f,g){'use strict';var h,i;if(i={},'string'==typeof f){if('object'==typeof g)for(h in g)i[h]=g[h];i.url=f}else if('object'==typeof f)for(h in f)i[h]=f[h];return i.type=i.type||d,i.oauthio={provider:a,tokens:b,request:c},e.http(i)}","post":"function (f,g){'use strict';var h,i;if(i={},'string'==typeof f){if('object'==typeof g)for(h in g)i[h]=g[h];i.url=f}else if('object'==typeof f)for(h in f)i[h]=f[h];return i.type=i.type||d,i.oauthio={provider:a,tokens:b,request:c},e.http(i)}"}

How you can see there are functions inside values. I want to rebuild this JavaScript object, my goal is to remove the quotes in the risult but also the values; because functions, in this case, are recognized as string. I want something like this:{key : value}

Now I get:{key : "value"}

3
  • 5
    With eval or Function (after some preprocessing). You can automate this by passing a reviver function to JSON.parse, just as explained in the MDN documentation. I strongly advice against storing functions as data though. Mar 25, 2015 at 16:12
  • I've tried both, but nothing . eval doesn't work because the values ​​missing the function name.
    – Claudio
    Mar 25, 2015 at 16:18
  • 1
    eval('(' + str + ')') Mar 25, 2015 at 16:35

2 Answers 2

5

QUICK ANSWER:

The function bellow will do it:

function fix(obj){

    for (var property in obj) {
        if (obj.hasOwnProperty(property)) {
            obj[property] = eval("(" + obj[property] + ")");
        }
    }

}

If obj has your JSON parsed object then just do the following:

fix(obj);
console.log(obj); // in case you want to see the change in the console

EXPLANATION (IF YOU NEED ONE):

You will be able to get the javascript function expression if you enclose the string with parentheses '()' before invoking eval.

So the steps to achieve the desired result are:

  1. Enclose the function expression string in parentheses (see footnotes for the reason why)
  2. Invoke the eval function to evaluate the function declaration expression
  3. Assign the function declaration expression to the same property that contained the string value

For the simplistic example you gave, you can get the desired results by:

var obj = {"get":"function (f,g){'use strict';var h,i;if(i={},'string'==typeof f){if('object'==typeof g)for(h in g)i[h]=g

[h];i.url=f}else if('object'==typeof f)for(h in f)i[h]=f[h];return i.type=i.type||d,i.oauthio=

{provider:a,tokens:b,request:c},e.http(i)}","post":"function (f,g){'use strict';var h,i;if(i={},'string'==typeof f){if

('object'==typeof g)for(h in g)i[h]=g[h];i.url=f}else if('object'==typeof f)for(h in f)i[h]=f[h];return i.type=i.type||

d,i.oauthio={provider:a,tokens:b,request:c},e.http(i)}"};

obj.get = eval("(" + obj.get + ")");
obj.post = eval("(" + obj.post + ")");

You can automate that by using the following function:

function fix(obj){

    for (var property in obj) {
        if (obj.hasOwnProperty(property)) {
            obj[property] = eval("(" + obj[property] + ")");
        }
    }

}

Your final code should be something like:

function fix(obj){

        for (var property in obj) {
            if (obj.hasOwnProperty(property)) {
                obj[property] = eval("(" + obj[property] + ")");
            }
        }

    }


var obj = {"get":"function (f,g){'use strict';var h,i;if(i={},'string'==typeof f){if('object'==typeof g)for(h in g)i[h]=g

    [h];i.url=f}else if('object'==typeof f)for(h in f)i[h]=f[h];return i.type=i.type||d,i.oauthio=

    {provider:a,tokens:b,request:c},e.http(i)}","post":"function (f,g){'use strict';var h,i;if(i={},'string'==typeof f){if

    ('object'==typeof g)for(h in g)i[h]=g[h];i.url=f}else if('object'==typeof f)for(h in f)i[h]=f[h];return i.type=i.type||

    d,i.oauthio={provider:a,tokens:b,request:c},e.http(i)}"};

fix(obj);

Footnotes:

In case you have interest to know why the parentheses are needed please check the link below:

Why does JavaScript's eval need parentheses to eval JSON data?

18
  • With your code I get this result: {get: function (), post: function()} Where am I wrong?
    – Claudio
    Mar 26, 2015 at 9:24
  • I focused on the functions , the code doesn't work because the function contains undefined variables that actually are defined outside the function , solutions?
    – Claudio
    Mar 26, 2015 at 9:49
  • It works at least with the example you provided, if you have other cases that don't work, please post them here so I can try to help you.
    – b-s-d
    Mar 26, 2015 at 13:55
  • Declaring a variable obj as you wrote, this code for me doesn't work: for(var property in obj){ if(obj.hasOwnProperty(property)){ obj[property] = eval("("+obj[property]+")"); } } ``console.log(obj); The risult is: { get: function (), post: function () }, try it.
    – Claudio
    Mar 26, 2015 at 14:30
  • The function statement is interpreted correctly and assigned to the variables as expected. Now if your function does not contain valid code, then for sure you will get an error if you try to execute it, in your case there are undefined variable being used. Fix the function and it will work. See this simple example: jsfiddle.net/d2bakop8
    – b-s-d
    Mar 26, 2015 at 14:39
4

this solution is better than using eval :

let's say obj contains your json, which means the function is obj.get but it is a sting, to convert it to a real function, you can use the constructor Function

obj.get = new Function(obj.get);

Note that I tried it on your code but the string you have posted has some errors on it. make sure your function is correct.

4
  • You might want to explain why it is better. Also this won't just work with the OP's code. Some preprocessing has to be done. Mar 25, 2015 at 16:33
  • Thanks for the help , if I wanted to put this code in a 'for' or 'while'?
    – Claudio
    Mar 26, 2015 at 9:57
  • do you mean your object will have many functions on it ? if so, is it going to have only function or other elements ?
    – Khalid
    Mar 26, 2015 at 10:28
  • I need automate that, if I had a long json file whose values ​​are all functions , I would first " transform" all in an object and then use it . Look at the other answer to get an idea of what I need. (Unfortunately that solution does not work for now).
    – Claudio
    Mar 26, 2015 at 14:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.