Search code examples
jsonevaljavascript

Why eval() isn't working here to deserialize such a simple JSON object?


I am trying to use eval() function to deserialize this JSON text by using eval function.

var personJSON = {
  "FirstName": "Burak",
  "LastName": "Ozdogan",
  "Id": "001",
  "Department": "Information Technologies"
};

var personBurakOzdogan = eval('(' + personJSON + ')');

But I am getting this error:

*Microsoft JScript compilation error: Expected ']'*

Is there something that I skip which I cannot catch?

Thanks


Solution

  • What you have is not JSON text. It is already a JSON object. So you don't need to use eval at all. You can directly access and manipulate its properties:

    alert(personJSON.FirstName);