122

I have the following...

JArray clients = (JArray)clientsParsed["objects"];

foreach (JObject item in clients.Children())
{
    // etc.. SQL params stuff...
    command.Parameters["@MyParameter"].Value = JTokenToSql(item["thisParameter"]);
}

JTokenToSql looks like this...

public static object JTokenToSql(JToken obj)
{
    if (obj.Any())
        return (object)obj;
    else
        return (object)DBNull.Value;
}

I have tried ((JObject)obj).Count also.. But doesn't seem to be working.

live-love
  • 48,840
  • 22
  • 240
  • 204
Kyle
  • 5,407
  • 6
  • 32
  • 47

6 Answers6

236

To check whether a property exists on a JObject, you can use the square bracket syntax and see whether the result is null or not. If the property exists, a JToken will be always be returned (even if it has the value null in the JSON).

JToken token = jObject["param"];
if (token != null)
{
    // the "param" property exists
}

If you have a JToken in hand and you want to see if it is non-empty, well, that depends on what type of JToken it is and how you define "empty". I usually use an extension method like this:

public static class JsonExtensions
{
    public static bool IsNullOrEmpty(this JToken token)
    {
        return (token == null) ||
               (token.Type == JTokenType.Array && !token.HasValues) ||
               (token.Type == JTokenType.Object && !token.HasValues) ||
               (token.Type == JTokenType.String && token.ToString() == String.Empty) ||
               (token.Type == JTokenType.Null);
    }
}
Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
  • 1
    I would make it extension method like: public static bool IsNullOrEmpty(this JToken token) { ... } to use like JToken token = jObject["param"]; bool empty = token.IsNullOrEmpty() – Dmitry Pavlov Jun 10 '15 at 16:29
  • 1
    Could you not `ToSrting` the `JToken` and check `IsNullOrWhiteSpace`? (After checking the `JToken` is not null of course) – Paul C Jul 14 '15 at 11:38
  • 1
    @CodeBlend That won't work for an object or an array -- the serialized versions of those when empty are `{}` and `[]` respectively. – Brian Rogers Jul 14 '15 at 14:07
  • 1
    I would add Property checking: return (token == null) || (token.Type == JTokenType.Array && !token.HasValues) || (token.Type == JTokenType.Object && !token.HasValues) || (token.Type == JTokenType.String && token.ToString() == String.Empty) || (token.Type == JTokenType.Null) || (token.Type == JTokenType.Property && ((JProperty)token).Value.ToString() == string.Empty); – jcmontx May 03 '19 at 17:22
85

You can proceed as follows to check whether a JToken Value is null

JToken token = jObject["key"];

if(token.Type == JTokenType.Null)
{
    // Do your logic
}
Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
Sam Ngugi
  • 1,103
  • 9
  • 13
  • 5
    Exactly what I was looking for, due to empty paramters returning a null type that passes a typical == null comparison. Thanks! – Tim Tyler Jul 18 '18 at 12:06
9

There is also a type - JTokenType.Undefined.

This check must be included in @Brian Rogers answer.

token.Type == JTokenType.Undefined
aleha_84
  • 8,309
  • 2
  • 38
  • 46
  • if it must be included in brians answer then id suggest you to post it as a comment in such answer. – Nande Jun 10 '22 at 04:52
3

As of C# 7 you could also use this:

if (clientsParsed["objects"] is JArray clients) 
{
    foreach (JObject item in clients.Children())
    {
        if (item["thisParameter"] as JToken itemToken) 
        {
            command.Parameters["@MyParameter"].Value = JTokenToSql(itemToken);
        }
    }
}

The is Operator checks the Type and if its corrects the Value is inside the clients variable.

Sebastian
  • 2,874
  • 25
  • 31
3

You can now in C# 6+ try to directly access with the null-conditional access operator ?[].

    foreach (JObject item in clients.Children())
    {
        // value will be null if access fails
        var value = (string)item?["thisParameter"]?["anotherNode"]?["oneMoreNestedNode"];
    } 
Nhan
  • 1,405
  • 1
  • 13
  • 16
1

Try something like this to convert JToken to JArray:

static public JArray convertToJArray(JToken obj)
{
    // if ((obj).Type == JTokenType.Null) --> You can check if it's null here

    if ((obj).Type == JTokenType.Array)
        return (JArray)(obj);
    else
        return new JArray(); // this will return an empty JArray
}
live-love
  • 48,840
  • 22
  • 240
  • 204