1

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?

2 Answers 2

3

I found the fix was to specify the following in the json formatter serializer settings:

jsonFormatter.SerializerSettings = new JsonSerializerSettings() 
{ 
   NullValueHandling = NullValueHandling.Include 
};
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

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.