Kavya Shetty
Kavya Shetty

Reputation: 315

JSONPatch WCF REST API getting invalid payload error

I am trying to use JSONPatch(KevinDockx version) in my WCF REST API(ASP.NET v4.5). My operation contract is as below:-

[OperationContract]
[WebInvoke(UriTemplate = "/{raceId}/participants", Method = "PATCH")]
void UpdateRace(string id, JsonPatchDocument<ParticipantContract[]> participantsContract);

And implementation as follows :-

public void UpdateRace(string id, JsonPatchDocument<ParticipantContract[]> participantsContract)
{
    //Someoperation
}

My data is like the below format where I want to perform add, update delete, move and swap operations on the participants array.

{
    "raceId" : 1
    "participants": [
        {
            "id": "abc",
            "car": "Mercedes",
            "model": "F1 W08 EQ Power",
            "teamname": "Mercedes-AMG Petronas Motorsport",
            "driver": {
                "id": "111",
                "firstname": "Lewis",
                "lastname": "Hamilton",
                "age": "29"
            },
            "codriver": {
                "id": "222",
                "firstname": "Valtteri",
                "lastname": "Bottas",
                "age": "32"
            }
        },
        {
            "id": "def",
            "car": "Ferrari",
            "model": "SF70H",
            "teamname": "Scuderia Ferrari",
            "borrower": {
                "id": "333",
                "firstname": "Sebastian",
                "lastname": "Vettel",
                "age": "30"
            },
            "coborrower": {
                "id": "444",
                "firstname": "Kimi",
                "lastname": "Räikkönen",
                "age": "37"
            }
        }
    ]
}

On JSON deserialization I am getting below error:-

{
    "summary": "Bad Request",
    "details": "Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'ParticipantContract' because the type requires a JSON object (e.g. {\"name\":\"value\"}) to deserialize correctly.\r\nTo fix this error either change the JSON to a JSON object (e.g. {\"name\":\"value\"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.\r\nPath '', line 1, position 1."
}

Can you help me with what I am missing in this? Is there something additional that needs to be done?

Upvotes: 0

Views: 806

Answers (1)

Mukesh Modhvadiya
Mukesh Modhvadiya

Reputation: 2178

As I can see your JSON object is not valid

    {
        "raceId" : 1  // you missed comma here
        "participants": [
         {

Just checking for any other mistake in your JSON object and passing a valid JSON will fix the error.

Upvotes: 0

Related Questions