Checking if JValue is null

check if json object is empty c#
newtonsoft check if token exists
c# check if json object is null or empty
create empty jtoken c#

Why this code doesn't run, I want to check if JSON contains integer for key PurchasedValue or not? () :

public PropertyInfo(Newtonsoft.Json.Linq.JToken jToken)
{
    this.jToken = jToken;
    int PurchasedValue = (int)(jToken["PurchasedValue"].Value ?? 0);
}

the error is :

Error CS0019: Operator `??' cannot be applied to operands of type `method group' and `int' (CS0019) 

From my understanding jToken["PurchasedValue"] is a nullable value. You have to use

int PurchasedValue = (int)(jToken["PurchasedValue"]?? 0);

nullableObj.Value can be only used without error only when there is a value for the nullableObj

Otherwise You can use like

int PurchasedValue = jToken["PurchasedValue"].HasValue?jToken["PurchasedValue"].Value: 0;

This May not even need type casting

Checking for empty/null JToken in a JObject, 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  It checks it using a null check using != null and isEmpty () method of string. In plain terms, if a string isn't a null and isEmpty () returns false, it's not either null or empty. Else, it is. However, the above program doesn't return empty if a string contains only whitespace characters (spaces).

Well, there are couple of things here :

The jToken["PurchasedValue"] could return anything so a type check would be preferable.

You can change your code as following:

public PropertyInfo(Newtonsoft.Json.Linq.JToken jToken)
{
    this.jToken = jToken;
    int PurchasedValue = jToken["PurchasedValue"] is int ? jToken["PurchasedValue"] : 0;
}

JToken IsNullOrEmpty | C# Online Compiler, Parse("[]")); // empty array. 10. Test(JToken.Parse("\"\"")); // empty string. 11. Test(​JToken.Parse("null")); // null value. 12. } 13. 14. private static void Test(JToken  A field with a NULL value is a field with no value. If a field in a table is optional, it is possible to insert a new record or update a record without adding a value to this field. Then, the field will be saved with a NULL value. Note: A NULL value is different from a zero value or a field that contains spaces.

You can compare the token type:

var purchasedValueToken = jToken["PurchasedValue"];
int purchasedValue = purchasedValueToken.Type == JTokenType.Null ? 0 : purchasedValueToken.Value<int>();

Casting JValue, WriteLine(u); // http://www.bing.com/ JValue v6 = JValue.CreateNull(); u = (Uri)v6; Console.WriteLine((u != null) ? u.ToString() : "{null}"); // {null} DateTime? dt  Assuming that these properties are reference types and could be null values. There are many ways to check for null and short circuit the evaluation. A good tip is to avoid using the .ToString() instance method and use the static method Convert.ToString() instead, it doesn't throw an exception if the passed value is null. The return value can

JValue.CreateNull Method, Why this code doesn't run, I want to check if JSON contains integer for key PurchasedValue or not? () : public PropertyInfo(Newtonsoft.Json.Linq.JToken jToken)  Shortest way to check for null and assign another value if not. // if Value is not null, newValue = Value else if Value is null newValue is YournullValue var

Checking if JValue is null - c# - csharp, [Test] public void EmbedJValueStringInNewJObject() { string s = null; var v = new JValue(s); var o = JObject.FromObject(new {title = v}); string Sign up for free to join this conversation on GitHub. Already have an account? There are situations, where you first check for the validity of a pointer, and if it is NULL, you just return/exit out of a function. (I know this can lead to the discussion "should a function have only one exit point") Most of the time, you check the pointer, then do what you want and then resolve the error case.

Using a null string JValue in a new JObject changes the value , if (c != null) { string name = (string)c["name"]; } this works fine If you want to test whether something is a JValue use logic like this instead:. So the null check might pass but before the code gets to doing anything with the value, it's changed - perhaps to null. I'm not positive if this is a concern or not (it might not be), but it seems worth watching out for.

Comments
  • It's of Newtonsoft JSON.NET
  • PurchasedValue = (int)(jToken["PurchasedValue"].Type!=JTokenType.Null ? jToken["PurchasedValue"].Value<int>(): 0);