Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 337
Claudio Avatar asked Oct 27 '25 18:10

Claudio


1 Answers

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?

like image 93
b-s-d Avatar answered Oct 29 '25 07:10

b-s-d



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!