POST request with JSON Body Serialization

10,100

You can try to write two split species models, one for Receive JSON Modle, another for Response JSON Model.

Receive Modle

public class Value
{
    public object Reference { get; set; }
    public string Key { get; set; }
    public int IssueNumber { get; set; }
}

public class Message
{
    public List<Value> value { get; set; }
}

public class ReciveModel
{
    public Message message { get; set; }
}

Response Model

public class QueueItem
{
    public string Key { get; set; }
    public int IssueNumber { get; set; }
}

public class ResponseModel
{
    public string newStatus { get; set; }
    public List<QueueItem> queueItems { get; set; }
}

Receive your JSON data and Deserialize to ReciveModel object, then let the data into another instance ResponseModel

final, you use JsonConvert.SerializeObject to serialize then instance to be JSON data.

var obj = JsonConvert.DeserializeObject<ReciveModel>(JsonData);
var res = new ResponseModel() {
    newStatus = "New",
    queueItems = obj.message.value.Select(x => new QueueItem() {
        IssueNumber = x.IssueNumber,
        Key = x.Key
    }).ToList()
};
var jsonResult = JsonConvert.SerializeObject(res);

Result

{"newStatus":"New","queueItems":[{"Key":"abc","IssueNumber":123},{"Key":"def","IssueNumber":345}]}

c# online

Note

There are two way can create model easily.

  • You can use Web Essentials in Visual Studio, use Edit > Paste special > paste JSON as class, you can easier to know the relation between Json and model.

  • If you can't use Web Essentials you can instead of use http://json2csharp.com/ online JSON to Model class.

You can try to use those models to carry your JSON Format.

Share:
10,100
chaotic
Author by

chaotic

Updated on June 04, 2022

Comments

  • chaotic
    chaotic almost 2 years

    I have one specific issue, which I'm not able to handle.

    I'm using HTTP Get API request and I'm getting JSON string, which I'm deserializing, this works perfectly. But then, I need to reuse just two pairs of this JSON file, but it needs to be pasted as JSON body for POST request. Let me show you the example:

    Output of GET API Request:

    {
      "message":{
          "value":[
             {
               "Reference":null,
               "Key":"abc",
               "IssueNumber":123
             },
             {
               "Reference":null,
               "Key":"def",
               "IssueNumber":345
             }
           ]
         }
       }
    

    So now Im able to deserialize this JSON string (i.e.: jsonString("value)(0)("Key") and I will get "abc").

    But now, I have no idea, how to serialize this deserialized object to use ReviewStatus and Key. This POST request JSON body looks like that:

    {
       "newStatus":"New"
       "queueItems": [
         {
            "Key":"abc"
            "IssueNumber":123
         },
         {
            "Key":"def"
            "IssueNumber":456
         }
      ]
    }
    

    For loop works for me, but in that case, I will do API call for each item instead of doing just one POST API call. What would be the best solution in your opinion? I was trying to use Newtonsoft.Json.JsonConvert (SerializeObject Method), but it didn't work for me as I expected. I'm pretty sure, that there needs to be something much easier, but I need your help.

    Thanks for any advice. Frantisek

  • chaotic
    chaotic over 5 years
    Hey D-Shih, thanks a lot for your detailed answer! It seems pretty straightforward and your Note will make my life much easier :). I will try it tomorrow and I hope, it will work :). Thanks.