Claudio
Claudio

Reputation: 45

Parse function inside Json

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"}

Upvotes: 4

Views: 12415

Answers (2)

b-s-d
b-s-d

Reputation: 5153

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?

Upvotes: 5

Khalid
Khalid

Reputation: 4808

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.

Upvotes: 4

Related Questions