456

I am having a problem parsing simple JSON strings. I have checked them on JSONLint and it shows that they are valid. But when I try to parse them using either JSON.parse or the jQuery alternative it gives me the error unexpected token o:

<!doctype HTML>
<html>
  <head>
  </head>
  <body>
    <script type="text/javascript">
      var cur_ques_details ={"ques_id":15,"ques_title":"jlkjlkjlkjljl"};
      var ques_list = JSON.parse(cur_ques_details);

      document.write(ques_list['ques_title']);
    </script>
  </body>
</html>

Note: I'm encoding my strings using json_encode() in PHP.

Andrew Li
  • 55,805
  • 14
  • 125
  • 143
Saurabh Sharma
  • 4,894
  • 2
  • 15
  • 14

8 Answers8

811

Your data is already an object. No need to parse it. The javascript interpreter has already parsed it for you.

var cur_ques_details ={"ques_id":15,"ques_title":"jlkjlkjlkjljl"};
document.write(cur_ques_details['ques_title']);
Dark Falcon
  • 43,592
  • 5
  • 83
  • 98
  • 12
    how to detect from jquery if data is already a valid json object? – mko Sep 15 '14 at 10:30
  • 2
    @mko: In this case, you don't. You know it is or you don't. Look at it and see if it conforms to the JSON specification. – Dark Falcon Sep 15 '14 at 12:49
  • 5
    @DarkFalcon i went with if (typeof data == 'object') { dostuff } to check if it is an json object or just a plain string – mko Sep 15 '14 at 13:38
  • 2
    Note that JSON is JavaScript Object Notation, so litteral JSON in javascript source is just a JS Object. We borrowed Javascript's object syntax for data transfer between programming languages because it was simple to use. – Filip Haglund Feb 25 '15 at 17:12
  • 3
    @FilipHaglund: Except the syntax for JSON is a lot more strict than the syntax for a JS object. For example, JS allows unquoted property names and JSON does not. – Dark Falcon Feb 25 '15 at 17:14
  • Thank you so much for this answer. I've been searching for this all evening! – David Nov 06 '15 at 01:48
72

Try parse so:

var yourval = jQuery.parseJSON(JSON.stringify(data));
14

Using JSON.stringify(data);:

$.ajax({
    url: ...
    success:function(data){
        JSON.stringify(data); //to string
        alert(data.you_value); //to view you pop up
    }
});
Andrew Li
  • 55,805
  • 14
  • 125
  • 143
Iyes boy
  • 305
  • 3
  • 6
  • 1
    Just calling `JSON.stringify` will do nothing with your data, the function actually returns your _now_ serialized data. – Tom Hofman Aug 15 '17 at 11:08
11

The source of your error, however, is that you need to place the full JSON string in quotes. The following will fix your sample:

<!doctype HTML>
<html>
    <head>
    </head>
    <body>
        <script type="text/javascript">
            var cur_ques_details ='{"ques_id":"15","ques_title":"jlkjlkjlkjljl"}';
            var ques_list = JSON.parse(cur_ques_details);
            document.write(ques_list['ques_title']);
        </script>
    </body>
</html>

As the other respondents have mentioned, the object is already parsed into a JS object so you don't need to parse it. To demonstrate how to accomplish the same thing without parsing, you can do the following:

<!doctype HTML>
<html>
<head>
</head>
    <body>
        <script type="text/javascript">
            var cur_ques_details ={"ques_id":"15","ques_title":"jlkjlkjlkjljl"};
            document.write(cur_ques_details.ques_title);
        </script>
    </body>
</html>
always-a-learner
  • 3,671
  • 10
  • 41
  • 81
PlantationGator
  • 815
  • 1
  • 13
  • 21
10

cur_ques_details is already a JS object, you don't need to parse it

Shuping
  • 5,388
  • 6
  • 43
  • 66
6

Response is already parsed, you don't need to parse it again. if you parse it again it will give you "unexpected token o". if you need to get it as string, you could use JSON.stringify()

Muhammad Soliman
  • 21,644
  • 6
  • 109
  • 75
6

I had the same problem when I submitted data using jQuery AJAX:

$.ajax({
   url:...
   success:function(data){
      //server response's data is JSON
      //I use jQuery's parseJSON method 
      $.parseJSON(data);//it's ERROR
   }
});

If the response is JSON, and you use this method, the data you get is a JavaScript object, but if you use dataType:"text", data is a JSON string. Then the use of $.parseJSON is okay.

Andrew Li
  • 55,805
  • 14
  • 125
  • 143
Saber
  • 71
  • 1
  • 1
1

I was seeing this unexpected token o error because my (incomplete) code had run previously (live reload!) and set the particular keyed local storage value to [object Object] instead of {}. It wasn't until I changed keys, that things started working as expected. Alternatively, you can follow these instructions to delete the incorrectly set localStorage value.

Community
  • 1
  • 1
alexkb
  • 3,216
  • 2
  • 30
  • 30
  • I had the same issue of `[object Object]`, my problem was that i was not storing the normal object rather i was storing DOM object. So, i got it done by extracting the useful values from the DOM object and storing them in an object, after that i converted that object to JSON. And then for getting the value i parsed that JSON object. – Waleed Naveed Oct 09 '18 at 06:55