Checking for empty or null JToken in a JObject

C#Sqljson.net

C# Problem Overview


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.

C# Solutions


Solution 1 - C#

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);
    }
}

Solution 2 - C#

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
}

Solution 3 - C#

There is also a type - JTokenType.Undefined.

This check must be included in @Brian Rogers answer.

token.Type == JTokenType.Undefined

Solution 4 - C#

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.

Solution 5 - C#

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
}

Solution 6 - C#

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"];
    } 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKyleView Question on Stackoverflow
Solution 1 - C#Brian RogersView Answer on Stackoverflow
Solution 2 - C#Sam NgugiView Answer on Stackoverflow
Solution 3 - C#alehaView Answer on Stackoverflow
Solution 4 - C#SebastianView Answer on Stackoverflow
Solution 5 - C#live-loveView Answer on Stackoverflow
Solution 6 - C#NhanView Answer on Stackoverflow