I have an API where I get JSON as an input and I want to check if the specified property and it's value exists in that JSON.
Note: The JSON is not generated from code but is typed by the user so I cannot validate the JSON while serializing.
Consider the following JSON
{
"id": 1,
"someProperties":
{
"property1": "abc",
"property2": ["zzz", "ccc"]
}
}
In someProperties, property1 and property2 both can either exists at the same time or anyone of them. So I want to check which all properties are present. And if present, whether that property has the valid value.
I tried the following code:
dynamic request = JsonConvert.DeserializeObject(JSONRequestData);
var X = request["someProperties"]["property1"];
Following are the 2 scenarios I have to check with their respective responses:
- If
"property1": "abc"is not present in JSON then I am gettingnullinX - If I put
property1in JSON without its value which will look something like this"property1": ,then I am gettingnull.
So how do I differentiate between property not being present in JSON and value of property not being present?