1

I have below code written to read JArray from a given JObject.

To my understanding, when the value of "tags" in JObject is null, IEnumerable should be initialized as empty.

IEnumerable<string> tags = eventPayload?["tags"]?.Values<string>() ?? Enumerable.Empty<string>();

However, this lines throws exception when json looks like

{
    "tags": null   
}

System.InvalidOperationException
  HResult=0x80131509
  Message=Cannot access child value on Newtonsoft.Json.Linq.JValue.

If I were to make above line read null and initialize enumerable as empty, what changes do I need to make?

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Evan Park
  • 528
  • 1
  • 6
  • 20

1 Answers1

2

The issue is that eventPayload?["tags"] is JValue.Null, not null.

Eugene Shvets
  • 4,561
  • 13
  • 19