So I created a WebApi project in MVC4 and was having trouble understanding the JSON output. I’m not entirely sure if this is the intended output of JSON or if i have done something incorrectly.
I have three tables, (pb_Project, pb_Tasks and pb_Priority). The relationship are as follows:
Project may have many tasks, and Each project can have one priority level.
entities http://img705.imageshack.us/img705/9294/stackoverflowentities.png
Now, the output that I am receiving is quite odd and not really intuitive. I’m not sure if this is the intended JSON (as I am brand new to it). When I run the website to retrieve the JSON output for the projects I get the following result.
[
{
"$id": "1",
"pb_Priority": {
"$id": "2",
"pb_Projects": [
{
"$ref": "1"
},
{
"$id": "3",
"pb_Priority": {
"$ref": "2"
},
"pb_Tasks": [
{
"$id": "4",
"pb_Projects": {
"$ref": "3"
},
"TASK_ID": 3,
"PROJECT_ID": 2,
"USER_ID": 2,
"DUE_DATE": "2012-11-19T00:00:00",
"PRIORITY_ID": 2,
"TASK_NAME": "project 2 task 1",
"DESCRIPTION": "asdf"
}
],
"PROJECT_ID": 2,
"USER_ID": 2,
"NAME": "Project2",
"DESCRIPTION": "project 2 description",
"BUDGET": 1,
"LOCATION": "CANADA",
"START_DATE": "2012-11-18T00:00:00",
"PRIORITY_ID": 1,
"PROJ_SCALE": null,
"NOTES": null
}
],
"PRIORITY_ID": 1,
"PRIORITY_LEVEL": 1
},
"pb_Tasks": [
{
"$id": "5",
"pb_Projects": {
"$ref": "1"
},
"TASK_ID": 1,
"PROJECT_ID": 1,
"USER_ID": 1,
"DUE_DATE": "2012-11-19T00:00:00",
"PRIORITY_ID": 1,
"TASK_NAME": "project 1 task 1",
"DESCRIPTION": "adsf"
},
{
"$id": "6",
"pb_Projects": {
"$ref": "1"
},
"TASK_ID": 2,
"PROJECT_ID": 1,
"USER_ID": 2,
"DUE_DATE": "2012-11-19T00:00:00",
"PRIORITY_ID": 1,
"TASK_NAME": "project 1 task 2 user 2",
"DESCRIPTION": "asd"
}
],
"PROJECT_ID": 1,
"USER_ID": 1,
"NAME": "Project1",
"DESCRIPTION": "project 1 description",
"BUDGET": 1000,
"LOCATION": "USA",
"START_DATE": "2012-11-18T00:00:00",
"PRIORITY_ID": 1,
"PROJ_SCALE": null,
"NOTES": null
},
{
"$ref": "3"
}
]
The controller for this is as follows:
public IEnumerable<pb_Projects> Getpb_Projects()
{
return db.pb_Projects.AsEnumerable();
}
What I would expect from this json result would be a list of Projects(objects) with any other relative FK’s and such. Whereas here I get one big object, with another ref as the 2nd object. Am i doing something wrong, i would expect something like this (don’t worry its just test data so no real passwords :P)…
[
{
"$id": "1",
"UserId": 1,
"CreateDate": "2012-11-18T21:46:56.78",
"ConfirmationToken": null,
"IsConfirmed": true,
"LastPasswordFailureDate": null,
"PasswordFailuresSinceLastSuccess": 0,
"Password": "AIGvGbJIedgTyY+m08DCVhZbVnRTWzW6kvl1JMOqsFqOCSjnJTBlGUGmVveAYIONJw==",
"PasswordChangedDate": "2012-11-18T21:46:56.78",
"PasswordSalt": "",
"PasswordVerificationToken": null,
"PasswordVerificationTokenExpirationDate": null
},
{
"$id": "2",
"UserId": 2,
"CreateDate": "2012-11-18T21:48:05.067",
"ConfirmationToken": null,
"IsConfirmed": true,
"LastPasswordFailureDate": null,
"PasswordFailuresSinceLastSuccess": 0,
"Password": "ACeoUpOVNEvgumvmSkFziRkNQzTlZC8+KAy32egXQseKLmhmJGX/VDXRXylkBUph3Q==",
"PasswordChangedDate": "2012-11-18T21:48:05.067",
"PasswordSalt": "",
"PasswordVerificationToken": null,
"PasswordVerificationTokenExpirationDate": null
},
{
"$id": "3",
"UserId": 3,
"CreateDate": "2012-11-19T00:35:49.163",
"ConfirmationToken": null,
"IsConfirmed": true,
"LastPasswordFailureDate": null,
"PasswordFailuresSinceLastSuccess": 0,
"Password": "AF7W+1FNLcaA2qJ3nmrB7S9mkJmJGZCpAZYbyH5+iXryq1epr8i67ddgSbvFQF1bQw==",
"PasswordChangedDate": "2012-11-19T00:35:49.163",
"PasswordSalt": "",
"PasswordVerificationToken": null,
"PasswordVerificationTokenExpirationDate": null
},
{
"$id": "4",
"UserId": 4,
"CreateDate": "2012-11-19T00:39:00.667",
"ConfirmationToken": null,
"IsConfirmed": true,
"LastPasswordFailureDate": null,
"PasswordFailuresSinceLastSuccess": 0,
"Password": "ABPdfSj8jZAWqhHMDuTVQuL3AiEmiplSujA/GNjQoGoMQm0zhNwAAOaInbqrXFrM0g==",
"PasswordChangedDate": "2012-11-19T00:39:00.667",
"PasswordSalt": "",
"PasswordVerificationToken": null,
"PasswordVerificationTokenExpirationDate": null
}
]
Here is the raw unformatted JSON
[{"$id":"1","pb_Priority":{"$id":"2","pb_Projects":[{"$ref":"1"},{"$id":"3","pb_Priority":{"$ref":"2"},"pb_Tasks":[],"PROJECT_ID":2,"USER_ID":2,"NAME":"test2","DESCRIPTION":"test2","BUDGET":1.0,"LOCATION":"test","START_DATE":"2012-11-18T00:00:00","PRIORITY_ID":1,"PROJ_SCALE":null,"NOTES":null}],"PRIORITY_ID":1,"PRIORITY_LEVEL":1},"pb_Tasks":[{"$id":"4","pb_Projects":{"$ref":"1"},"TASK_ID":1,"PROJECT_ID":1,"USER_ID":1,"DUE_DATE":"2012-11-19T00:00:00","PRIORITY_ID":1,"TASK_NAME":"asdf","DESCRIPTION":"adsf"},{"$id":"5","pb_Projects":{"$ref":"1"},"TASK_ID":2,"PROJECT_ID":1,"USER_ID":2,"DUE_DATE":"2012-11-19T00:00:00","PRIORITY_ID":1,"TASK_NAME":"sdf","DESCRIPTION":"asd"},{"$id":"6","pb_Projects":{"$ref":"1"},"TASK_ID":3,"PROJECT_ID":1,"USER_ID":2,"DUE_DATE":"2012-11-19T00:00:00","PRIORITY_ID":2,"TASK_NAME":"dsf","DESCRIPTION":"asdf"}],"PROJECT_ID":1,"USER_ID":1,"NAME":"test","DESCRIPTION":"test","BUDGET":null,"LOCATION":"test","START_DATE":"2012-11-18T00:00:00","PRIORITY_ID":1,"PROJ_SCALE":null,"NOTES":null},{"$ref":"3"}]
If you are using EF, by default, it will lazy load all the data from navigation properties. When serializer serializes model, it will visit each property on your model and EF will load all nested data.
As a best practice, you shouldn’t expose your DB model as web api model. You’d better create another DTO model which you can control exactly what will be returned to client and what you want to receive from client.
Or you can quickly workaround it by hiding those navigation properties by modifier (mark them as internal), or using [IgnoreDataMember] attribute. It tells serializer not to visit them when serializing and deserilaizing.