I have Json file, where i am trying the parse the data of the json file for database update with some data contions, Currently i want to check an particular element exists or not.In my current Json data, Under the Operation Node the accessRef can exist in the file or may not exist completely.So how would check the condtion. Kindly suggest me.
Below is the Json file
{
"PLMXML":{
"language":"en-us",
"time":"16:50:15",
"schemaVersion":"6",
"author":"Development(-2069033203)",
"date":"2020-05-22",
"Header":{
"id":"id1",
"traverseRootRefs":"#id6",
"transferContext":"CLM"
},
"Operation":{
"id":"id21",
"name":"PD_Op2",
"subType":"BS4_BaOP",
"accessRefs":"#id18",
"catalogueId":"70700000209604",
"ApplicationRef":{
"application":"Teamcenter",
"label":"yDX58d4FIpN8QD",
"version":"yDX58d4FIpN8QD"
}
}
}
}
Below the Code which i am trying perform
public void Readjsonfile(string jsondata)
{ var message = JsonConvert.DeserializeObject<plmxmldatamodel>(jsondata);
Console.WriteLine("RootRef: " + message.PLMXML.Header.traverseRootRefs);
Console.WriteLine("OccurenceId: "+message.PLMXML.ProductView.Operation.id);
// I want to check whtether is accessrefs is present or not inside the Operation element
}
//fuction1
public class Header
{ public string traverseRootRefs { get; set; }
}
//fuction2
public class Operation
{ public string id { get; set; }
public string name { get; set; }
public string subType { get; set; }
public string accessRefs { get; set; }
public string catalogueId { get; set; }
}
//fuction3
public class PLMXML
{
public string language { get; set; }
public Header Header { get; set; }
public Operation Operation { get; set; }
}
//fuction4
public class plmxmldatamodel
{
public PLMXML PLMXML { get; set; }
}
If empty/null value for accessRefs
is a valid situation (i.e. ... "'accessRefs':, ..."
) then you can use JSON Path to check if it exists:
var jObj = JObject.Parse(jsondata);
if(jObj.SelectToken("$.PLMXML.Operation.accessRefs") != null)
{
// do what you want to do if accessRefs exists
}
Otherwise just check for message.PLMXML.ProductView.Operation.accessRefs
not being null:
if(message.PLMXML.ProductView.Operation.accessRefs != null)
{
// do what you want to do if accessRefs exists
}