Solved

handling JSON when null values appear

Posted on 2015-02-04
2
163 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 33

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 33

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Suggested Courses

739 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