1

I am having an issue when read JSON returned from my ASP.NET webservice to the client. The JSON returned contains an ObjectId generated in MongoDB database, and I need it in the client.

The problem start when I run

 var res = jQuery.parseJSON(data.d)[0]; // array of companies

I get the error:

Uncaught SyntaxError: Unexpected token O 

I assume is that the Javascipt JSON parser doesn't recognize the ObjectId.

In my .NET C# code I seralized and return JSON:

 var jsonWriterSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict };
return results.ToJson(jsonWriterSettings);

The return JSON string from the webservice:

[{ "companies" : [{ "id" : ObjectId("53e129ed0000000000000000"), "name" :
 "Company 1" }, { "id" : ObjectId("53e12a290000000000000000"), "name" :
 "Company 2" }, { "id" : ObjectId("53e12a650000000000000000"), "name" :
 "Company 3" }] }]"

How can I use the ObjectId in Javascript. I do need the string of the ObjectId in some of the HTML elements (e.g. drop down list values)?

Update: I found out about the BSON seralizer here. However, I don't know if that's the appropriate solution.

2
  • Can you please post an example of the raw JSON code you receive from your webservice? Commented Aug 5, 2014 at 8:12
  • @Philipp, Hi, I've added it to the question body. Thanks Commented Aug 5, 2014 at 8:18

3 Answers 3

2

I would usually not recommended to expose an ObjectId to the user, because a 24-digit hex string is not very user-friendly. But when you still want to do this, you should be able to convert it to a string with its toString method.

The JSON example code you posted is not valid JSON, so it's no miracle that parsing on the client-side fails. That means you need to find a different tool (or maybe you are just not using it properly) to generate your JSON or convert the ObjectId's to strings before converting it.

Sign up to request clarification or add additional context in comments.

3 Comments

Do you recommend that I just use a integer which I auto increment myself? (those lists are short, max 50 items)
@IdanShechter In order to provide useful UI design advise I would need a more complete view of your whole user interface.
The list like the company list will be used to populate a drop down list, that's all, and I have a few of those. If I use int as the Id, I can query the document to the amount of items and add +1 to the next item. The insertion of new data will only be available for the admin, only I can add new companies, so it shouldn't be intensive on the database, especially if it's a short list and happens once in a while.
0

You need to change your web services so it returns:

[{ "companies" : [{ "id" : "53e129ed0000000000000000", "name" :
 "Company 1" }, { "id" : "53e12a290000000000000000", "name" :
 "Company 2" }, { "id" : "53e12a650000000000000000", "name" :
 "Company 3" }] }]"

Otherwise it is invalid JSON

Comments

-1
var userid=results[i].id;

Get objectId Value

1 Comment

Welcome to Stack Overflow! While this code may help with the question, it is better to include some context, explaining how it works and when to use it. Code-only answers tend to be less useful in the long run. See How do I write a good answer? for some more info.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.