1

I need to pass the following JSON object to my ASP.NET Core 3.1 Web API... how can I access this object from the API?

{
    "client": {
        "clientID": "3529",
        "clientData": "This a test data"
    },
    "selectedUserIDs": [
        3549,
        3567,
        3546,
        3561,
        3532       
    ],
    "selectedDepartmentsIDs": [
        10695,
        71,
        72,
        121     
       
    ]
}

Here is my API controller

[HttpPost("/api/client/{id}/savedata")]
public ActionResult SaveClientDetails(int workflowID, [FromBody] Clientdata data)
{            
    //save operation        
}

and my various classes:

public class clientdata
{
    public client client{ get; set; }
    public List<selectedUserIDs> selectedUserIDs{ get; set; }
    public List<selectedDepartmentsIDs> selectedDepartmentsIDs { get; set; }
}

public class client 
{
    public string clientID { get; set; }
    public string clientData { get; set; }
}

public class selectedUserIDs
{        
    public int Ids{ get; set; }
}

public class selectedDepartmentsIDs
{        
    public int Ids{ get; set; }
}

I am not able to access this complex object in my Web API body. In Postman, I'm seeing the following error:

{
    "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
    "title": "One or more validation errors occurred.",
    "status": 400,
    "traceId": "|10eabbbb-4a15387d9152b7d6.",
    "errors": {
        "$.selectedUserIDs[0]": [
            "The JSON value could not be converted to System.Collections.Generic.List`1[ClientCoreAPI.Client.Entity.selectedUserIDs]. Path: $.selectedUserIDs[0] | LineNumber: 6 | BytePositionInLine: 12."
        ]
    }
}

1 Answer 1

2

Your definition of clientdata does not match the json file. You have two options:

  1. Change clientdata to:

     public class clientdata
     {
         public client client{ get; set; }
         public List<int> selectedUserIDs{ get; set; }
         public List<int> selectedDepartmentsIDs { get; set; }
     }
    
  2. Or change the json to

    {
        "client": {
            "clientID": "3529",
            "clientData": "This a test data"
        },
        "selectedUserIDs": [
            { "Ids": 3549 },
            { "Ids": 3567 },
            { "Ids": 3546 },
            { "Ids": 3561 },
            { "Ids": 3532 }       
        ],
        "selectedDepartmentsIDs": [
            { "Ids": 10695 },
            { "Ids": 71 },
            { "Ids": 72 },
            { "Ids": 121 }
        ]
    }
    
Sign up to request clarification or add additional context in comments.

1 Comment

i can not modify that input json which is coming from already running application. any other way on api side

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.