263
votes

I parse few data using a type class in my controller I'm getting data as follows:

{  
   "data":{  
      "userList":[  
         {  
            "id":1,
            "name":"soni"
         }
      ]
   },
   "status":200,
   "config":{  
      "method":"POST",
      "transformRequest":[  
         null
      ],
      "transformResponse":[  
         null
      ],
      "url":"/home/main/module/userlist",
      "headers":{  
         "rt":"ajax",
         "Tenant":"Id:null",
         "Access-Handler":"Authorization:null",
         "Accept":"application/json, text/plain, */*"
      }
   },
   "statusText":"OK"
}

I tried to store the data like this

var userData = _data;
var newData = JSON.parse(userData).data.userList;

How can I extract the user list to a new variable?

10
You may need not use JSON.parse. Try using userData directly as an object.Mohit Bhardwaj
If console.log(typeof userData) shows object then you already have an javascript object and not a JSON string you need to parse.t.niese
@MohitBhardwaj yes, need not required for the parse..Soniya Mohan
Usually whenever you get this error - Unexpected token o in JSON, most probably you are trying to parse an object which is already in parsed form.Mohit Bhardwaj
@MohitBhardwaj okay!Soniya Mohan

10 Answers

370
votes

The JSON you posted looks fine, however in your code, it is most likely not a JSON string anymore, but already a JavaScript object. This means, no more parsing is necessary.

You can test this yourself, e.g. in Chrome's console:

new Object().toString()
// "[object Object]"

JSON.parse(new Object())
// Uncaught SyntaxError: Unexpected token o in JSON at position 1

JSON.parse("[object Object]")
// Uncaught SyntaxError: Unexpected token o in JSON at position 1

JSON.parse() converts the input into a string. The toString() method of JavaScript objects by default returns [object Object], resulting in the observed behavior.

Try the following instead:

var newData = userData.data.userList;
106
votes

the first parameters of function JSON.parse should be a String, and your data is a JavaScript object, so it will convert to a String [object object], you should use JSON.stringify before pass the data

JSON.parse(JSON.stringify(userData))
35
votes

Don't ever use JSON.parse without wrapping it in try-catch block:

// payload 
let userData = null;

try {
    // Parse a JSON
    userData = JSON.parse(payload); 
} catch (e) {
    // You can read e for more info
    // Let's assume the error is that we already have parsed the payload
    // So just return that
    userData = payload;
}

// Now userData is the parsed result
28
votes

Just above JSON.parse, use:

var newData = JSON.stringify(userData)
3
votes

Unexpected 'O' error is thrown when JSON data or String happens to get parsed.

If it's string, it's already stringfied. Parsing ends up with Unexpected 'O' error.

I faced similar( although in different context), I solved the following error by removing JSON Producer.

    @POST
    @Produces({ **MediaType.APPLICATION_JSON**})
    public Response login(@QueryParam("agentID") String agentID , Officer aOffcr ) {
      return Response.status(200).entity("OK").build();

  }

The response contains "OK" string return. The annotation marked as @Produces({ **MediaType.APPLICATION_JSON})** tries to parse the string to JSON format which results in Unexpected 'O'.

Removing @Produces({ MediaType.APPLICATION_JSON}) works fine. Output : OK

Beware: Also, on client side, if you make ajax request and use JSON.parse("OK"), it throws Unexpected token 'O'

O is the first letter of the string

JSON.parse(object) compares with jQuery.parseJSON(object);

JSON.parse('{ "name":"Yergalem", "city":"Dover"}'); --- Works Fine

2
votes

Well, I meant that I need to parse object like this: var jsonObj = {"first name" : "fname"}. But, I don't actually. Because it's already an JSON.

2
votes

We can also add checks like this:

function parseData(data) {
    if (!data) return {};
    if (typeof data === 'object') return data;
    if (typeof data === 'string') return JSON.parse(data);

    return {};
}
2
votes

You can simply check the typeof userData & JSON.parse() it only if it's string:

var userData = _data;
var newData;
if (typeof userData === 'object')
  newData = userData.data.userList; // dont parse if its object
else if (typeof userData === 'string')
  newData = JSON.parse(userData).data.userList; // parse if its string
1
votes

Give a try catch like this, this will parse it if its stringified or else will take the default value

let example;
   try {
   example  = JSON.parse(data)
  } catch(e) {
    example = data
  }
-2
votes

var data = JSON.parse(userData); console.log(data);