1

How can I check if any key from json object have null value

  JsonObject itemObject = itemValue.GetObject();   

  string id = itemObject["id"].GetString() == null ? "" : itemObject["id"].GetString();

  this is my code but app crashes on it if null value for key "id"
Dattatray Deokar
  • 1,334
  • 1
  • 16
  • 29

4 Answers4

4
IJsonValue idValue = itemObject.GetNamedValue("id");

if ( idValue.ValueType == JsonValueType.Null)
{
    // is Null
}
else if (idValue.ValueType == JsonValueType.String)
{
    string id = idValue.GetString();
}

If you do this too much, consider adding extension methods.

To do the opposite use:

IJsonValue value = JsonValue.CreateNullValue();

Read here more about null values.

kiewic
  • 14,245
  • 11
  • 71
  • 90
0

if itemObject["id"] is null then the method null.GetString() doesn't exist and you'll get the error specified (null object never has any methods/fields/properties).

string id = itemObject["id"] == null ? (string)null : itemObject["id"].GetString(); // (string)null is an alternative to "", both are valid null representations for a string, but you should use whichever is your preference consistently to avoid errors further down the line

the above avoids calling .GetString() until you've asserted that the ID isn't null (check here for more in-depth), if you're using C#6 you should be able to use the new shorthand:

string id = itemObject["id"]?.GetString();
Community
  • 1
  • 1
Dead.Rabit
  • 1,914
  • 1
  • 25
  • 44
  • 1) It makes no sense to use the null conidional operator after ["id"], since JsonObject's indexer never returns null. Though it would throw KeyNotFoundException if there is no such property in your JSON. 2) (string)null is not a null representation for a string. Null is null, and "" is not null - it is a string of zero length. – Yarik Nov 17 '15 at 17:02
  • 1) KeyNotFoundException is reason enough. 2) (string)null compiles. – Dead.Rabit Nov 17 '15 at 17:11
  • Ok, so I can't press enter in a comment.. the exception you receive from (2) if you omit the (string) cast is to do with how C# unboxes variables. In a nutshell it's because null is typeless the compiler will throw a wobbly. - that is the technical term FYI – Dead.Rabit Nov 17 '15 at 17:13
  • There is no boxing or unboxing, string is always a reference type, so it doesn't need to be boxed. There will be no exception thrown in (2), but rather a compilation error. Moreover, in C# 6.0 you don't need to cast a null in the ternary operator (?:), the compiler is smart enough to infer the resulting type automatically. – Yarik Nov 18 '15 at 22:43
0

http://msdn.microsoft.com/en-us/library/ms173224.aspx

The ?? operator is called the null-coalescing operator. It returns the left-hand operand if the operand is not null; otherwise it returns the right hand operand.

Cassian
  • 3,318
  • 1
  • 25
  • 37
  • both sides of the null-coalescing operator must be of the same type so I think the OP would need to write code like this? `string id = ( itemObject["id"] ?? Guid.Empty ).GetString();` – Dead.Rabit Dec 08 '14 at 14:15
  • try string id = ( itemObject["id"].GetString() ?? " " ); – Cassian Dec 08 '14 at 14:34
0

Here is solution for the issue

string id = itemObject["id"].ValueType == JsonValueType.Null ? "" : itemObject["id"].GetString();

Dattatray Deokar
  • 1,334
  • 1
  • 16
  • 29