0

I am sending a API request to an endpoint and receiving a response.

I have an object RootResponse This object is never a list and will always be a single object.

Within this object is an object called Package and within that, an object called Activity.

This activity object represents the information when a carrier scans a package. However sometimes there may be only 1 scan on a package in which case when I deserialize my JSON response into my RootResponse object i get the following error

Batchtracker.Models.TrackingAPI.Response.Activity[]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type

I have tried changing the type of object Activity to a List<Activity> and Activity[] but still an error. I understand that the issue is that when there is only one activity scan, it's only returning as a single object and not an array. How can i fix this issue?

Here is an example of the response activity object before deserialization when there is only one scan

"Activity": {
  "ActivityLocation": {
    "Address": {
      "CountryCode": "US"
    }
  },
  "Status": {
    "Type": "M",
    "Description": "Order Processed: Ready for UPS",
    "Code": "MP"
  },
  "Date": "20190426",
  "Time": "135618"
},

and heres the entire response for any curious

{
  "TrackResponse": {
    "Response": {
      "ResponseStatus": {
        "Code": "1",
        "Description": "Success"
      },
      "TransactionReference": ""
    },
    "Shipment": {
      "InquiryNumber": {
        "Code": "01",
        "Description": "ShipmentIdentificationNumber",
        "Value": "redacted"
      },
      "ShipperNumber": "redacted",
      "ShipmentAddress": [{
        "Type": {
          "Code": "01",
          "Description": "Shipper Address"
        },
        "Address": {
          "AddressLine": "redacted redacted redacted redacted",
          "City": "redacted",
          "StateProvinceCode": "GA",
          "PostalCode": "redacted",
          "CountryCode": "US"
        }
      }, {
        "Type": {
          "Code": "02",
          "Description": "ShipTo Address"
        },
        "Address": {
          "City": "redacted",
          "StateProvinceCode": "redacted",
          "PostalCode": "redacted",
          "CountryCode": "US"
        }
      }],
      "ShipmentWeight": {
        "UnitOfMeasurement": {
          "Code": "LBS"
        },
        "Weight": "2.00"
      },
      "Service": {
        "Code": "003",
        "Description": "UPS Ground"
      },
      "ReferenceNumber": {
        "Code": "01",
        "Value": "redacted"
      },
      "PickupDate": "20190426",
      "DeliveryDetail": {
        "Type": {
          "Code": "03",
          "Description": "Scheduled Delivery"
        },
        "Date": "20190430"
      },
      "Package": {
        "TrackingNumber": "redacted",
        "PackageServiceOption": {
          "Type": {
            "Code": "010",
            "Description": "Hundredweight"
          }
        },
        "Activity": {
          "ActivityLocation": {
            "Address": {
              "CountryCode": "US"
            }
          },
          "Status": {
            "Type": "M",
            "Description": "Order Processed: Ready for UPS",
            "Code": "MP"
          },
          "Date": "20190426",
          "Time": "135618"
        },
        "PackageWeight": {
          "UnitOfMeasurement": {
            "Code": "LBS"
          },
          "Weight": "2.00"
        },
        "ReferenceNumber": {
          "Code": "01",
          "Value": "redacted"
        }
      }
    }
  }
}
boo
  • 129
  • 1
  • 4
  • 12
  • 1
    I hate APIs that do that. Are you in control of the API to change it? The return value should have a consistent structure. Otherwise, I think your only option is to parse individual values instead of deserializing the whole thing into an object. – itsme86 Apr 29 '19 at 19:24
  • Unfortunately no it's UPS api :\ I wonder if their response in XML is any difference. – boo Apr 29 '19 at 19:27
  • Do a quick search of the json string before you deserialize it, to see if the Activity object is there and has data – Alex M Apr 29 '19 at 19:28
  • The object does have data. – boo Apr 29 '19 at 19:29
  • What library are you using to deserialize json? – tukaef Apr 29 '19 at 19:41
  • This is a very complex object. What are you trying to deserialize this into? – Alex M Apr 29 '19 at 19:41
  • I am using newtonsoft . I generated classes using jsonToCsharp. The response is being deserialized into my RootResponse object. – boo Apr 29 '19 at 19:43
  • Just running quickly this json through the jsontocsharp I can see that the representation is not what I would like. For example it created Type, Type2, Type3 classes, when in fact this would be a List, I think you would have to analyze the classes carefully and make sure that the classes match the objects. Maybe check documentation to see if they have class structure you need. – Alex M Apr 29 '19 at 19:54
  • I saw that as well and did not create Type2, Type3, I left it as Type as they are the same objects. – boo Apr 29 '19 at 20:21
  • There is a very good answer to this problem in the answer that itsme86 tagged. Thank you. – boo Apr 29 '19 at 20:37

0 Answers0