1

I have an SSIS C# script component that hits an API, pulls down the response, and I'm working on deserializing it into a proper C# object.

Here's the basic JSON that the API returns:

{
    "id":1234,
    "answers": [
        "answer one"
    ]
}

Here's the corresponding C# class:

internal class ApiResponse {
     public string id { get; set; }
     public string[] answers { get; set; }
}

Then from using System.Web.Script.Serialization I declare a JavaScriptSerializer and use it to deserialize the response:

JavaScriptSerializer js = new JavaScriptSerializer();
ApiResponse apiResponse = js.Deserialize<ApiResponse>(rawJsonFromApi);

This works perfectly for most inputs, but in some cases, the "answers" array is empty, and the API returns a string that confirms that:

{
    "id":2345,
    "answers": "The user did not provide any answers."
}

What can I do to make the JavaScriptSerializer expect to see either an array of strings or just a string so that it can process both JSON objects with one method/set of code?

  • 1
    JavaScriptSerializer was born before I was.. Do you have to use it? – Caius Jard Mar 16 '22 at 19:01
  • @CaiusJard Since submitting the question, I've rewritten the script component to use [DataContractJsonSerializer](https://learn.microsoft.com/en-us/dotnet/api/system.runtime.serialization.json.datacontractjsonserializer?view=netframework-4.7.2) and the DataContract's DataMembers expect object, which can take either a string or a string[]. Working much better with a few more wrinkles to iron out, but I'm working on it. SO etiquette: Should I delete my question, or answer it myself? – Joe Ireland Mar 16 '22 at 20:30
  • Er.. I probably wouldn't use DCJS either :D - System.Text.Json is the modern serializer - you would probably want to look at something like https://stackoverflow.com/questions/59430728/how-to-handle-both-a-single-item-and-an-array-for-the-same-property-using-system – Caius Jard Mar 16 '22 at 20:51
  • !!! Alright, I might rewrite it again. Another note - how does one determine what the most modern standard is in cases like this? I feel like it's easy to see in JavaDocs when a class or library was first introduced, but .NET is a little opaque on this. – Joe Ireland Mar 16 '22 at 21:26
  • Correction - The reason I avoided System.Text.Json is that it doesn't seem to be available in the .NET framework 4.8 I'm using; it's only a part of the full-blown .NET 6. – Joe Ireland Mar 16 '22 at 21:32
  • I see; Newtonsoft.Json would be the popular option for .NetFW. As to determining what's modern - it's tricky. Sometimes you find it on MSDN - for example it advises [not to use FtpWebRequest](https://docs.microsoft.com/en-us/dotnet/api/system.net.ftpwebrequest?view=net-6.0) in the remarks section. Another route can be to search nuget for e.g. Json and look at the download counters - 1 billion plus for Newtonsoft for example :). If using NS, posts like https://stackoverflow.com/questions/5224697/deserializing-json-when-sometimes-array-and-sometimes-object can help with this "type x or type y" – Caius Jard Mar 17 '22 at 06:49

0 Answers0