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
Solved

handling JSON when null values appear

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

839 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