0

I have a json in the form of { a:1, b:10, c:43 } for example.

I wish to perform eval( "(a+b-5)*c" ) but applying it to the json, not the place where the json and the formula is.

Attempted this, trying to utilize the scope, but wouldn't find a.

var z = { a:1, b:10, c:43, eval:eval };
console.log( z.eval( "a+b" ) );
Discipol
  • 3,137
  • 4
  • 22
  • 41
  • 3
    I do not understand what you mean... but if you mean what I think you do, there'll be no way but to unpack the JSON, do the array, and re-pack it. – Pekka Oct 05 '13 at 00:04
  • what array are you talking about? – Discipol Oct 05 '13 at 00:07
  • This: `{ a:1, b:10, c:43 }` array, object, same difference in this case... it'd look something like `result = (myJSON.a+myJSON.b-5)*myJSON.c)` – Pekka Oct 05 '13 at 00:09
  • how would one "unpack" it? – Discipol Oct 05 '13 at 00:10
  • Well, where does the JSON come from? Can you show some code? – Pekka Oct 05 '13 at 00:10
  • In no way encouraging this, but: `var json = { a:1, b:10, c:43 }; with (json) { eval( "(a+b-5)*c" ) }` – go-oleg Oct 05 '13 at 00:12
  • This json is the sum of several jsons with all (maybe some missing) properties, on which I must apply a formula as a string and I can guarantee that all the variables in that formula exist in the json – Discipol Oct 05 '13 at 00:12
  • @go-oleg I would accept your answer should you post it like that. It worked for my purposes. also I can add several evals within that `with`. – Discipol Oct 05 '13 at 00:20
  • @Discipol: Looks like vkurchatkin has a very similar answer. – go-oleg Oct 05 '13 at 00:54

4 Answers4

2

You can use the with keyword, but please don't. Using with and eval is not recommended.

var z = { a:1, b:10, c:43 };
with(z) {
    console.log(eval('a+b'));
}

Here's some more info on MDN.

DarthJDG
  • 16,511
  • 11
  • 49
  • 56
  • then how do I scope inside the json without `with`? – Discipol Oct 05 '13 at 00:39
  • 1
    You can use ```with``` if you like, the whole approach as it is, is not recommended. If I would need to do something like this, I'd go back and re-think what I want to achieve and if there's a better way to do it. Although I'm not saying I've never done messy things myself due to lack of time or interest. ;) – DarthJDG Oct 05 '13 at 00:44
1

Check this out:

function expression (expr) {
    return new Function('obj', 'with (obj) return ' + expr);
}

console.log(expression('(a+b-5)*c')({ a:1, b:10, c:43 }));
vkurchatkin
  • 13,364
  • 2
  • 47
  • 55
0

Dont use eval! USE JSON.parse

JSON.parse

JSON.parse vs. eval()

var obj=JSON.parse(json);

alert((obj.a+obj.b-5)*obj.c)
Community
  • 1
  • 1
zloctb
  • 10,592
  • 8
  • 70
  • 89
0

I guess you want something like:

var yourJSon = '{ "a":1, "b":10, "c":43 }'; 
//Your original JSon String (attention to the standard double quotes)

yourJSon = JSON.parse(yourJSon); 
//this will render your JSon string into a "real" object

var answer = eval("(yourJSon.a + yourJSon.b - 5) * yourJSon.c"); 
//And now you can do the eval, using the object's variable to define the scope of that object.
Marcelo Myara
  • 2,841
  • 2
  • 27
  • 36