I am sending the Json format like this in PostMan
    {
  "number": 2106887,
  "date": "09/10/2018",
  "degree":"BE"
  "Students": [
    {
      "Branch": "ABK015",
      "Doc": "NCE",
      "Description": "Testing",
      "dni": "1016035232",
      "Name": "ABCE",
      "Gender": "M",
      "Title": "Univercity",
      "email": "[email protected]",
    },
    {
      "Branch": "ABK016",
      "Doc": "NCE",
      "Description": "Testing1",
      "dni": "1016035233",
      "Name": "ABCE",
      "Gender": "M",
      "Title": "Univercity",
      "email": "[email protected]",
    }
  ]
}
In controller level i am doing the validation of all fields. After validation how can i convert the above json string to below format
{
  "Students": [
    {
      "number": 2106887,
      "date": "09/10/2018",
      "degree":"BE"
      "Branch": "ABK015",
      "Doc": "NCE",
      "Description": "Testing",
      "dni": "1016035232",
      "Name": "ABCE",
      "Gender": "M",
      "Title": "Univercity",
      "email": "[email protected]",
    },
    {
      "number": 2106887,
      "date": "09/10/2018",
      "degree":"BE"
      "Branch": "ABK016",
      "Doc": "NCE",
      "Description": "Testing1",
      "dni": "1016035233",
      "Name": "ABCE",
      "Gender": "M",
      "Title": "Univercity",
      "email": "[email protected]",
    }
  ]
}
And aftre converting, i want to insert to database. How to convert in c#? Please help me.
And the below code is the class for students:
public class Students
{
    [Required]
    public string Branch{ get; set; }
    [Required]
    public string Doc{ get; set; }
    [Required]
    public string Description{ get; set; }
    [Required]
    public string dni{ get; set; }
    [Required]
    public string Name{ get; set; }
    [Required]
    public string Gender{ get; set; }
    [Required]
    public string Title{ get; set; }
    [Required]
    public string email{ get; set; }
    [Required]
    public string degree{ get; set; }
    [Required]
    public string date{ get; set; }
    [Required]
    public string number{ get; set; }
}
And i am deserializing here
var requestBody = requestContent.Content.ReadAsStringAsync().Result;
            JObject jxxx = JsonConvert.DeserializeObject<dynamic>(requestBody);
Please refer the updated code
You can use DeserializeAnonymousType method. First create a template object
var template = new {number = "", date = "", degree = "", Students = new Students[0]};
now deserialize to temporary json object:
var jsonObject = JsonConvert.DeserializeAnonymousType(input, template);
After this you can copy info from json header to Students with linq:
var students = jsonObject.Students.Select(s =>
{
    s.number = jsonObject.number;
    s.date = jsonObject.date;
    s.degree = jsonObject.degree;
    return s;
}).ToArray();
Also note, that your input JSON is not valid: it missing comma afetr degree value.
Demo is here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With