Difference between JSON.parse() and eval() Difference between JSON.parse() and eval() json json

Difference between JSON.parse() and eval()


Your problem is that you mixing two unrelated things.

eval() is built-in javascript function, which main purpose is to interpret string of javascript code (thus make potentional security hole)

JSON.parse() function is for parse JSON string. Although very simmilar, do not make mistake, JSON is not Javascript and there are tiny differences. You should not use eval() for parsing JSON

What are the differences between JSON and JavaScript object?


$eval is automatically evaluated against a given scope.

For example:

$scope.a = 2;var result = $scope.$eval('1+1+a');// result is 4

$parse does not require scope. It takes an expression as a parameter and returns a function. The function can be invoked with an object that can resolve the locals:

For example:

var fn = $parse('1+1+a');var result = fn({ a: 2 });// result is 4


When you use eval for parsing JSON you need to wrap your expression with parentheses

eval('(' + s2 + ')');

jsfiddle