0

I have a JSON response like the following

{
    "result": "success",
    "totalresults": "100",
    "items": {
        "item": [            
            {
                "id": "5812",                
                "lineitems": {
                    "lineitem": [
                        {
                            "type": "product",                            
                            "status": "Active"
                        }
                    ]
                }
            },
            {
                "id": "5",               
                "lineitems": []
            }
          ]
    }
}

While trying to deserialize this with a specific type it throws exception because of the lineitems property. lineitems is an empty array for one item and for the other item it has a property lineitem with an array. I don't have control over this JSON data. Please suggest me how to deserialize this without any error.

Using lineitems as object would help me to deserialize the JSON but then I'll end up having 2 different types in the object field which is not gonna help me either.

dev
  • 1
  • 2
  • 1
    You probably need a custom converter, see [this answer](https://stackoverflow.com/questions/58715272/json-complex-type-that-can-be-an-object-or-an-array-of-objects/58715918#58715918) for an example on how to do that. – DavidG Feb 02 '21 at 12:37
  • Please share with us the related piece of code, the exception and point out where did you stuck. – Peter Csala Feb 02 '21 at 12:46
  • 2
    Does this answer your question? [How to handle both a single item and an array for the same property using JSON.net](https://stackoverflow.com/questions/18994685/how-to-handle-both-a-single-item-and-an-array-for-the-same-property-using-json-n) – Drag and Drop Feb 02 '21 at 13:08
  • @Dev, What do you expect btw? You have talk about what you got but did not state anything about the expected result. You are fine with `lineitems` being a `List` or you want it to be a single object? In the second case I will have an other dupe target. – Drag and Drop Feb 02 '21 at 13:31
  • @DragandDrop I want either lineitem or lineitems as an array – dev Feb 02 '21 at 13:53
  • Got lost in the naming, but I simply add the Custom converter and the Array when you had a single object https://dotnetfiddle.net/uGSxI8. For some reason My brain wrote the previous comment in french for no reason. Sorry about that. – Drag and Drop Feb 02 '21 at 14:14
  • @dev Did you even look at the two links provided here? They BOTH give you an answer that works. – DavidG Feb 02 '21 at 14:18
  • The magic of simple copy past and ~+5 minutes. the longest part was to make that json into a string lliterral. @DavidG, Just give up already and swing that hammer. Can't you hear the crowd? The Json C# tag is just full of simple dupe target. – Drag and Drop Feb 02 '21 at 15:00
  • 1
    @DragandDrop Ask and ye shall receive! – DavidG Feb 02 '21 at 15:06

1 Answers1

-2

If you are sure about "lineitems", You can do the replace policy. Once you get the JSON string from API, Do a condition based replace

json = json.Replace("lineitems: []", "lineitems: null");

Once you do it, Your JSON will be standardized. Then you can do desterilize.

Sangeeth Nandakumar
  • 1,362
  • 1
  • 12
  • 23