Solved

handling JSON when null values appear

Posted on 2015-02-04
2
147 Views
Last Modified: 2015-02-04
I'm building up a .NET service that consumes JSON from a 3rd party service and I'm using JSON.net to parse it out into it's own class. This works perfectly fine when all the fields are present, but if any of them return a blank or null value, it errors out. Below is a segment of the JSON being returned:

“Status”:
{
    "status": "Shipped",
    "tracking_number": [
        {
            "sku": "3346",
            "trackingNumber": "273436470000723                                   "
        },
        {
            "sku": "4186",
            "trackingNumber": "273436470000723                                   "
        }
} ]

Open in new window


there are times when the tracking number will be blank, it'll look like:

“Status”:
{
    "status": "Shipped",
    "tracking_number": []
}

Open in new window


and here's my class definition:

    public class CCG_ReceivedOrders
    {
        public string order_id                          { get; set; }
        public CCG_ReceivedOrderStatus status           { get; set; }
    }

    public class CCG_ReceivedOrderStatus
    {
        public string status                            { get; set; }
        public CCG_TrackingNumbers tracking_number      { get; set; }
    }

    public class CCG_TrackingNumbers
    {
        public int sku                                  { get; set; }
        public string tracking_number                   { get; set; }
    }

Open in new window


if I comment out CCG_TrackingNumbers.tracking_Number, it works fine. if that field contains a value, it works fine, so my only thought is that it is not handling the null value properly. The exact error message I'm getting is:

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'CCG_Data_Objects.CCG_TrackingNumbers' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Path '[0].status.tracking_number', line 1, position 922.

Open in new window


any help is greatly appreciated!
0
Comment
Question by:Big Monty
  • 2
2 Comments
 
LVL 32

Author Comment

by:Big Monty
ID: 40589110
I found the NullValueHandling attribute that json.net supports, and changed my parsing code to

return JsonConvert.DeserializeObject<List<T>>(jsonData,new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore});

but i still get the same error. is the json data format bad?
0
 
LVL 32

Accepted Solution

by:
Big Monty earned 0 total points
ID: 40589183
figured out the solution, I needed to return a list of tracking numbers, so I changed my code to:

    public class CCG_ReceivedOrderStatus
    {
        public string status                             { get; set; }
        public List<CCG_TrackingNumbers> tracking_number { get; set; }
    }

Open in new window

0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now