6

I'm using JSON.parse() to parse a json that's being returned from an api (Laravel 5) called using jquery's $.get(). The json is seemingly valid, however, JSON.parse() is returning error in both Safari and Chrome.

Chrome says:

Uncaught SyntaxError: Unexpected token o

Safari says:

SyntaxError: JSON Parse error: Unexpected identifier "object"

The code fragment is as below:

    $.get('/foo/' + product_id, function(data){
        console.log(data);
        var product = JSON.parse(data);
        if (product) {
            // do something
        }
     });

The JSON is:

{  
   "id":"1b7b3eb7-8769-48fe-a421-64c105de3eff",
   "parent":null,
   "org_id":"845d0d53-de68-42c3-9007-c3d0e72c555e",
   "category_id":"e58237f7-e040-4098-8d46-b84f8cdf7d83",
   "purchase_tax":null,
   "sale_tax":null,
   "code":"982",
   "name":"Mr. Destin Hoppe",
   "is_purchased":false,
   "is_sold":false,
   "purchase_price":null,
   "selling_price":null,
   "purchase_includes_tax":false,
   "sale_includes_tax":false,
   "created_at":"2015-09-16 17:39:34",
   "updated_at":"2015-09-16 17:39:34"
}

Interestingly, eval() works just fine.

Drew Gaynor
  • 8,292
  • 5
  • 40
  • 53
Code Poet
  • 11,227
  • 19
  • 64
  • 97

1 Answers1

14

The error is a result of data being an object, not JSON. You don't need to parse anything; it is already a JavaScript object. jQuery does the parsing within its get method. To confirm this, add this line to the top of the callback.

console.log(data["id"]);

As another example of this error, the following line will also fail for the same reason.

JSON.parse({});
Drew Gaynor
  • 8,292
  • 5
  • 40
  • 53
  • Since when? Agreed I'm using jquery after years, but last I used it, it used to be a string (or was it?) – Code Poet Sep 17 '15 at 16:47
  • 1
    @CodePoet It's been that way for years. – Álvaro González Sep 17 '15 at 16:48
  • Ah. I think I see it now, the Response header says `Content-Type: application/json`, maybe that's why? – Code Poet Sep 17 '15 at 16:48
  • You can identify the response type in the client-side call and/or the server-side code. Perhaps you never did thus jQuery didn't have enough information to do the job for you. – Álvaro González Sep 17 '15 at 16:49
  • @CodePoet: From the [docs](http://api.jquery.com/jquery.ajax/): *"Different types of response to $.ajax() call are subjected to different kinds of pre-processing before being passed to the success handler. The type of pre-processing depends by default upon the Content-Type of the response, but can be set explicitly using the dataType option."* – Felix Kling Sep 17 '15 at 16:52
  • @FelixKling Right. I have used `dataType` in the past to ensure that jQuery knows JSON is coming back. Alternatively, the `getJSON` method could be used, which is shorthand for the same: http://api.jquery.com/jquery.getjson/. – Drew Gaynor Sep 17 '15 at 16:55
  • I'm pretty sure the api's I was using then, weren't setting `Content-Type`, because I wrote those api's too. Silly me :) – Code Poet Sep 17 '15 at 17:03