Parsing JSON giving "unexpected token o" error [duplicate]
Asked Answered
H

8

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.

Hemmer answered 25/3, 2013 at 14:16 Comment(1)
Changed it to: var ques_list = JSON.stringify( cur_ques_details); Thanks.Codee
G
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']);
Glasswort answered 25/3, 2013 at 14:17 Comment(6)
how to detect from jquery if data is already a valid json object?Mosul
@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.Glasswort
@DarkFalcon i went with if (typeof data == 'object') { dostuff } to check if it is an json object or just a plain stringMosul
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.Misspend
@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.Glasswort
Thank you so much for this answer. I've been searching for this all evening!Expansible
B
72

Try parse so:

var yourval = jQuery.parseJSON(JSON.stringify(data));
Burkley answered 14/7, 2015 at 15:32 Comment(3)
This worked for me for debugging. Thanks a lot. I realized I was echoing out extra unnecessary info from my controller.Conklin
Why use jQuery?Derinna
Why not use JSON.parse?Derinna
Q
14

Using JSON.stringify(data);:

$.ajax({
    url: ...
    success:function(data){
        JSON.stringify(data); //to string
        alert(data.you_value); //to view you pop up
    }
});
Quadrinomial answered 25/2, 2015 at 17:8 Comment(1)
Just calling JSON.stringify will do nothing with your data, the function actually returns your now serialized data.Delineation
S
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>
Shipper answered 25/3, 2013 at 14:30 Comment(0)
M
10

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

Magnetostriction answered 25/3, 2013 at 14:17 Comment(2)
There's no such thing as a "JSON object". JSON is a string. What you mean is a "JS object".Flite
I think what he meant is "Javascript object"Iceboat
E
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()

Eudemon answered 29/9, 2014 at 20:54 Comment(0)
W
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.

Weinrich answered 8/10, 2014 at 7:17 Comment(0)
S
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.

Syllabary answered 22/2, 2015 at 6:38 Comment(1)
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.Clite

© 2022 - 2024 — McMap. All rights reserved.