Skip to content
Advertisement

Checking for empty or null JToken in a JObject

I have the following…

JTokenToSql looks like this…

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

Advertisement

Answer

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).

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:

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement