Reputation: 2863
I've got the following following command in my web api:
return Request.CreateResponse(HttpStatusCode.OK,
MyDBContext.DB.Database.SqlQuery<MyCustomerClass>("SELECT * FROM CUSTOMER").ToList());
Here is the table:
CREATE TABLE [dbo].[Customer] (
[CustomerID] [int] NOT NULL,
[FirstName] [nvarchar](50) NOT NULL,
[LastName] [nvarchar](50) NULL,
CONSTRAINT [PK_Customer] PRIMARY KEY CLUSTERED
([CustomerID] ASC) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
ON [PRIMARY]
) ON [PRIMARY]
GO
I have found that when requesting data from the webApi, if the fields are null
then the returning JSON result doesn't include that field in the return result. Is this expected behaviour?
Upvotes: 1
Views: 1404
Reputation: 2863
I found the fix was to specify the following in the json formatter serializer settings:
jsonFormatter.SerializerSettings = new JsonSerializerSettings()
{
NullValueHandling = NullValueHandling.Include
};
Upvotes: 3
Reputation: 1906
I don't think that's expected behavior. At least in terms of JSON. I can't speak much about WebAPI, since I haven't used it. In a project of mine that uses JSON, if I were to issue the following code:
# Package our response into an array...
$response = array("type"=>"remove_from_distribution_list","results");
# And send it back to XMLHttpRequest object encoded...
echo json_encode($response);
with results having no value, results would still be passed along. There would just be no value passed alongside it.
Upvotes: 1