0
{"InvPhy":[{"OpID":234,"RtID":null,"AccID":5965,"LocID":7223,"DlvrLocID":7738,"InvAccID":31345,"FscDt":"2019-05-16T00:00:00","TrDT":"2019-09-19T18:13:17+05:30","Schdl":true,"CntType":661,"DvcID":56000,"SvcUID":0,"Dtls":[{"ProdID":48456,"ProdPkgID":84947,"TrDT":"2019-09-19T18:13:09+05:30","Qty":2},{"ProdID":32382,"ProdPkgID":61198,"TrDT":"2019-09-19T18:13:17+05:30","Qty":3}]}]}

I have this JSON and I need to remove property RtID.

JObject jo = JObject.Parse(jsonResult);
jo.Property("RtID").Remove();
jsonResult = jo.ToString();

This is not working...Pls help

Stefan
  • 652
  • 5
  • 19
MainakChoudhury
  • 502
  • 1
  • 7
  • 23
  • Possible duplicate of [How to ignore a property in class if null, using json.net](https://stackoverflow.com/questions/6507889/how-to-ignore-a-property-in-class-if-null-using-json-net). (It may help, but I am not quite sure if it will fit) – aloisdg Sep 20 '19 at 08:20

4 Answers4

0

You need use remove method and pass field name.

jo.Remove("RtID");
cdev
  • 5,043
  • 2
  • 33
  • 32
0

One option would be as follow:

Consider you have the following C# classes to deserialize your JSON object to:

public class Dtl
{
    public int ProdID { get; set; }
    public int ProdPkgID { get; set; }
    public DateTime TrDT { get; set; }
    public int Qty { get; set; }
}

public class InvPhy
{
    public int OpID { get; set; }
    //public object RtID { get; set; }
    public int AccID { get; set; }
    public int LocID { get; set; }
    public int DlvrLocID { get; set; }
    public int InvAccID { get; set; }
    public DateTime FscDt { get; set; }
    public DateTime TrDT { get; set; }
    public bool Schdl { get; set; }
    public int CntType { get; set; }
    public int DvcID { get; set; }
    public int SvcUID { get; set; }
    public List<Dtl> Dtls { get; set; }
}

public class RootObject
{
    public List<InvPhy> InvPhy { get; set; }
}

You could just remove the RtID property, and it would not deserialize the value of that property.

Here is an example of how you could deserialize your JSON to an object. Deserializing to an object also makes it easier to work with in the rest of your code.

RootObject account = JsonConvert.DeserializeObject<RootObject>(json);
monstertjie_za
  • 7,277
  • 8
  • 42
  • 73
0

You probably need something like this:

   JObject jo = JObject.Parse(jsonResult);
   JToken token = jo.GetValue("InvPhy");
   JArray array = (token as JArray);
   JToken ob = array[0];
   (ob as JObject).Remove("RtID");
0

This worked for me:

JsonSerializerSettings settings = new JsonSerializerSettings();
            settings.NullValueHandling = NullValueHandling.Ignore;
            string jsonResult = JsonConvert.SerializeObject(ModelObbject, settings);
MainakChoudhury
  • 502
  • 1
  • 7
  • 23