-1

So my API service provider has inconsistent JSON response body elements. Specifically, a property that can contain one or many records isn't always passed back as an array. If there are multiple records it is. But if there is only one record then the record is passed back as a singleton.

What I am looking to do in my C# app is to programmatically append the [ ] around the singleton record, so that it's correctly defined as an array with a single element. More of a regex experiment, since I am handling the JSON response body as a string. I know it's perhaps less efficient, but the JSON string is what a third-party "black box" is looking for.

Example of the singleton JSON response body.

{
   "Transfer":{
      "transferID":"3581",
      "sent":"true",
      "received":"true"
   }
}

And now an example of the array JSON body.

{
   "Transfer":[
      {
         "transferID":"3581",
         "sent":"true",
         "received":"true"
      },
      {
         "transferID":"3582",
         "sent":"true",
         "received":"true"
      }
   ]
}

Just looking for the quickest and cleanest way to add the [ and the ] around the singleton Transfer record. Would like a reusable method, since the API service provider passes back other record types this way as well.

gregarican
  • 105
  • 1
  • 11
  • 1
    first, i would _very strongly_ recommend fixing the API to provide a well-structured response and fix or throw away all 3rd-party black boxes that require this broken behavior. if that's not an option, i still wouldn't use a regex for this - you'd end up with more problems than you started. instead, i'd try serializing it to the array version and, if that fails, to the single-object (_not_ singleton) version. – Franz Gleichmann Dec 30 '20 at 16:29
  • 2
    alternatively, [this question](https://stackoverflow.com/questions/18994685/how-to-handle-both-a-single-item-and-an-array-for-the-same-property-using-json-n) might provide a solution- – Franz Gleichmann Dec 30 '20 at 16:36
  • Franz, that link you provided pretty much did the trick. Wish I could control what constraints were placed on this project. But this worked out fine as a workaround. Thanks! – gregarican Dec 30 '20 at 17:52

1 Answers1

1

Apparently the most intuitive way to handle the inconsistency is described here --> How to handle both a single item and an array for the same property using JSON.net.

Specifically, here is the C# class that facilities what's required.

class SingleOrArrayConverter<T> : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return (objectType == typeof(List<T>));
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        JToken token = JToken.Load(reader);
        if (token.Type == JTokenType.Array)
        {
            return token.ToObject<List<T>>();
        }
        return new List<T> { token.ToObject<T>() };
    }

    public override bool CanWrite
    {
        get { return false; }
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}
gregarican
  • 105
  • 1
  • 11