Solved

handling JSON when null values appear

Posted on 2015-02-04
2
172 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

628 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