skip to Main Content
{
  "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?

4

Answers


  1. Chosen as BEST ANSWER

    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.


  2. 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

    Login or Signup to reply.
  3. 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}");
         }
    }
    
    Login or Signup to reply.
  4. 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() };
            }
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search