title: How to handle both a single item and an array for the same property using System.Text.Json?
body: <p>I am trying to deserialize some JSON that contains a value that is sometimes an array, and sometimes a single item.  How can I do this with <a href="https://docs.microsoft.com/en-us/dotnet/api/system.text.json" rel="noreferrer"><code>System.Text.Json</code></a> and <a href="https://docs.microsoft.com/en-us/dotnet/api/system.text.json.jsonserializer" rel="noreferrer"><code>JsonSerializer</code></a>?  (This question is inspired by <a href="https://stackoverflow.com/q/18994685/3744182">this question</a> for Json.NET by <a href="https://stackoverflow.com/users/403765/robert-mclaws">Robert McLaws</a>.)</p>
  
  <p>I have received the following JSON:</p>
  
  <pre><code>[
    {
      "email": "john.doe@sendgrid.com",
      "timestamp": 1337966815,
      "category": [
        "newuser",
        "transactional"
      ],
      "event": "open"
    },
    {
      "email": "jane.doe@sendgrid.com",
      "timestamp": 1337966815,
      "category": "olduser",
      "event": "open"
    }
  ]
  </code></pre>
  
  <p>And I want to deserialize it to a list of the following type:</p>
  
  <pre><code>class Item
  {
      public string Email { get; set; }
      public int Timestamp { get; set; }
      public string Event { get; set; }
  
      public List&lt;string&gt; Category { get; set; }
  }
  </code></pre>
  
  <p>Using the following code:</p>
  
  <pre><code>var options = new JsonSerializerOptions
  {
      PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
  };
  var list = JsonSerializer.Deserialize&lt;List&lt;Item&gt;&gt;(json, options);
  </code></pre>
  
  <p>However, when I do I get the following exception:</p>
  
  <blockquote>
    <pre class="lang-none prettyprint-override"><code>System.Text.Json.JsonException: The JSON value could not be converted to System.String. Path: &gt; $[1].category | LineNumber: 13 | BytePositionInLine: 25.
     at System.Text.Json.ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(Type propertyType)
     at System.Text.Json.JsonPropertyInfo.Read(JsonTokenType tokenType, ReadStack&amp; state, Utf8JsonReader&amp; reader)
     at System.Text.Json.JsonSerializer.ReadCore(JsonSerializerOptions options, Utf8JsonReader&amp; reader, ReadStack&amp; readStack)
     at System.Text.Json.JsonSerializer.ReadCore(Type returnType, JsonSerializerOptions options, Utf8JsonReader&amp; reader)
     at System.Text.Json.JsonSerializer.Deserialize(String json, Type returnType, JsonSerializerOptions options)
     at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonSerializerOptions options)
  </code></pre>
  </blockquote>
  
  <p>The exception occurs because the value of <code>"category"</code> is sometimes a single string, and sometimes an array of strings.  How can I deserialize such a property with <code>System.Text.Json</code>?</p>