0
{
  "id": "zd_tags",
  "value": [
    "bp_ticket;pazure_server"
  ]
},
{
  "id": "itd_priority_1",
  "value": 900
},
{
  "id": "impacted_services",
  "value": [
    "Serverdown0208_2"
  ]
},

I am unable to read and deserialize the JSON into my objects which throws the below error.

"$.incidentTags[6].value": [
  "The JSON value could not be converted to System.String[]. Path: $.incidentTags[6].value | LineNumber: 0 | BytePositionInLine: 1856."
]

Below is my current structure to deserialize it.

 public class  incidentTag
{
public string id { get; set; }
public string[] value {get;set;}

 }

Here is the problem, Some tag has no array " "id": "itd_priority_1" but many have an array of value.

What is the best way to handle this scenario?

  • 2
    So, sometimes `value` is an array of strings, other times an integer? You'll need to make it `object` or `dynamic` in your code and then figure out what to do inside the code by checking the type of the deserialized object – Flydog57 Aug 02 '22 at 15:22

4 Answers4

1

Cant make a comment so having to make an answer.

Previously I have used a dynamic for if the data is unstructured or I don't know the data structure. An example of how I have used a dynamic is below

FeedResponse<dynamic> resultSet = await feedIterator.ReadNextAsync();
                foreach (dynamic item in resultSet)
                {
                    tasks.Add(_targetContainer.CreateItemAsync(item.Something));
                }

I presume similar could be applied to your use case

Tagz97
  • 87
  • 5
1

Either you can create a custom json converter to handle the parsing the way you want it, there are many examples you can look for it.

Or as an altenative you can use JObject, something like this, parse the json and visit the JPproperty in JObject and check whether that is array or object or any other primitive type.

JObject jObject = JObject.parse(your json);
foreach (var property in jObject.Properties())
{
   VisitToken(property.Value);
}    

private void VisitToken(JToken token)
{
    switch (token.Type)
    {
        case JTokenType.Object:
             VisitJObject(token.Value<JObject>());
             break;

        case JTokenType.Array:
             VisitArray(token.Value<JArray>());
             break;

        case JTokenType.Integer:
             case JTokenType.Float:
             case JTokenType.String:
             case JTokenType.Boolean:
             case JTokenType.Bytes:
             case JTokenType.Raw:
             case JTokenType.Null:
                  VisitPrimitive(token);
                  break;    
         default:
                  throw new FormatException($"Invalid JSON token: {token}");
     }
}
Navoneel Talukdar
  • 4,393
  • 5
  • 21
  • 42
1

I usually create a json constructor in this case

List<incidentTag> incidentTags = JsonConvert.DeserializeObject<List<incidentTag>>(json);

public class incidentTag
{
    public string id { get; set; }
    public string[] value { get; set; }
    [JsonConstructor]
    public incidentTag(JToken value)
    {
        if (value.Type.ToString() == "Array") this.value = value.ToObject<string[]>();
        else
        {
            this.value = new string[] { value.ToString() };
        }
    }
}
Serge
  • 40,935
  • 4
  • 18
  • 45
0

Old Post for fixing this issue

The Above link helped me to resolve the problem and one more thing is, need to add the reference for "Microsoft.AspNetCore.Mvc.NewtonsoftJson;".I guess, .Net core 6 is using the System.text.Json which has an issue or does not support this operation.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 05 '22 at 22:28