skip to Main Content

As mentioned in the title I have a variable of type Jproperty? How can I identify if it is empty?

I can check if is NULL but not able to check if it is empty

2

Answers


  1. You could check if its null or empty

    If (string.IsNullOrEmpty(yourJpropertyVariable.Value.ToString) {};
    

    Hope this helps.
    I am new to stack overflow so sorry for the indendation.

    Login or Signup to reply.
  2. Assuming that you are talking about Newtonsoft Json.NET library then:

    Checking for null can be done by checking JTokenType.Null:

    var jObject = JObject.Parse("""
    {
        "propEmpty": "",
        "propNull": null
    }
    """);
    var nullProp = jObject.Descendants().OfType<JProperty>().First(property => property.Name == "propNull");
    var isNull = nullProp.Value.Type == JTokenType.Null;
    

    For empty string you can try something like:

    var empty = jObject.Descendants().OfType<JProperty>().First(property => property.Name == "propEmpty");
    var isEmptyValue = empty.Value.ToString() == string.Empty;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search