RSS

JSON.parse() vs eval()

12 Feb

Hi Folks,

Here are some important points which will give the clear differentiation about why we go for JSON.parse() instead eval().

1. JSON is a subset of Javascript and json.parse() just parses JSON whereas eval would leave the door open to all javascript expressions.

2. All JSON.parse() implementations most likely use eval().

3. Not all browsers have native JSON support so there will be times where we need to use eval() to the JSON string.
Use JSON parser from or as that makes everything easy for us.
eval() is an evil but against some browsers its a necessary evil but where you can avoid it,do so.

4. If we parse the JSON with eval, we are allowing the string being parsed to contain absolutely anything, so instead of just being a set of data, you could find yourself executing function calls, or whatever also JSON’s parse accepts an aditional parameter, reviver, that lets you specify how to deal with certain values, such as datetimes

5. The eval function is very fast. However, it can compile and execute any JavaScript program, so there can be security issues. The use of eval is indicated when the source is trusted and competent. It is much safer to use a JSON parser.

Happy Coding!

Have a nice day.

Advertisements
 
Leave a comment

Posted by on February 12, 2013 in Uncategorized

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: