Web API Not Returning NULL Properties in JSON

When calling a web api where there are null values, the return object drops the property. I have read many articles on-line that talk about the problem being fixed with the new release but it did not work for me. After looking around and testing a few options I found a very easy fix. The below code is a sample code from a real application.

For Each t In NewsData
       NewsItemList.Add(New NewsList(Date.ToString("MM/dd/yyyy"), t._id, t.Caption, _
                                     t.Article, t.PublishPeriod, t.isActive))
Next
.....
.....
.....
Public Class NewsList
    Public Property DatePosted As Date
    Public Property _id As Integer
    Public Property Caption As String
    Public Property Article As String
    Public Property PublishPeriod As String
    <Required()>
     Public Property isActive As Boolean

     Public Sub New(ByVal DatePosted As Date, ByVal _id As Integer, ByVal Caption As String, _
                   ByVal Article As String, ByVal PublishPeriod As String, ByVal isActive As Boolean)

        Me.DatePosted = DatePosted
        Me._id = _id
        Me.Caption = Caption
        Me.Article = Article
        Me.PublishPeriod = PublishPeriod
        Me.isActive = isActive
    End Sub

The important part of these lines of code is adding a ‘Required()’ attribute. (Line 14)

    <Required()>

Looking for quality web hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , , , , , , | Posted under Programming/Coding | RSS 2.0

Author Spotlight

David Bauernschmidt

David Bauernschmidt

I live in the historical triangle of Virginia where I am married with two daughters. I have spent over 13 years working for a Fortune 500 company in the computer area. I started in VB 6.0 and by the time I ended my employment I was supervising a development team where we built many web applications. When my first daughter was born I wanted to spend more time with her so I left and became a programmer analyst for local government as well as launch my own company. Since then I have grown James River Webs into a profitable web design and application company helping small businesses create a big presence on the internet. As an employee I have created web application used by citizens and other companies. I enjoy fly fishing, and spending time with my family. I also enjoy learning new approaches and development tools when it comes to developing applications.

One Comment on Web API Not Returning NULL Properties in JSON

  1. The problem might be caused by having PHP magic quotes on.

    Ensure this configuration is in your php.ini:
    magic_quotes_gpc = Off

Leave a Reply

Your email address will not be published. Required fields are marked *


1 × 1 =

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>