Ignore case in JArray C#

c# json deserialize case insensitive
jtoken selecttoken case insensitive
jarray selecttoken
jmespath case insensitive
c# linq selecttoken
query jtoken c#
c# json net selecttoken
json net case sensitive deserialize

I have response which I am converting to to JArray. But due to case sensitivity I am not able to get the values. I am using field Unknown but from response it is coming UnKnown, so am getting null result.

var jlist = JArray.Parse(response);
reasonId = jlist.FirstOrDefault(i => i.Value<string>("AbsentReasonCD").Contains("Unknown"))
    .Value<string>("AbsentReasonId");

This is my data

 {
    "AbsentReasonId": 121,
    "AbsentReasonCD": "UnKnown"
  }

How can I solve this issue?

You could try to make the string lower case before checking:

var jlist = JArray.Parse(response);
reasonId = jlist.FirstOrDefault(i => i.Value<string>("AbsentReasonCD")
    .ToLower().Contains("unknown")).Value<string>("AbsentReasonId");

JSON.NET JObject key comparison case-insensitive, c# allows you to use dictionaries with keys that are case insensitive, so a workaround I've used is to convert the JObject to a dictionary with  Possible Duplicate: How to add a case-insensitive option to Array.IndexOf. int index1 = Array.IndexOf(myKeys, "foot"); Example I have FOOT in my array list, but it will return value of index1 = -1.

You can use IndexOf instead which has an overload that takes a comparer:

i.Value<string>("AbsentReasonCD")
 .IndexOf("Unknown", StringComparison.InvariantCultureIgnoreCase) != -1

Provide a way to do case-sensitive property deserialization · Issue , Could you provides a way to opt-in to case-sensitive property During merge/​unmerge works on JObject tree, comparaisons are made case sensitives not deserialize the property at all) when going from JSON to C# object. Problems with case insensitive deserialization [BinanceTradeOrderData] #136. Case-insensitive deserialization. During deserialization, Newtonsoft.Json does case-insensitive property name matching by default. The System.Text.Json default is case-sensitive, which gives better performance since it's doing an exact match. For information about how to do case-insensitive matching, see Case-insensitive property matching.

If you want to find elements that actually match Unknown in a case insensitive manner you can use String.Equals with the StringComparison.InvariantCultureIgnoreCase parameter :

var jlist = JArray.Parse(response);
var reasonId = jlist.FirstOrDefault(i =>i.Value<string>("AbsentReasonCD")
                                         .Equals("Unknown",StringComparison.InvariantCultureIgnoreCase))
                    .Value<string>("AbsentReasonId");

or

var jlist = JArray.Parse(response);
var mode = StringComparison.InvariantCultureIgnoreCase;
var reasonId = jlist.FirstOrDefault(i =>i.Value<string>("AbsentReasonCD")
                                         .Equals("Unknown",mode))
                    .Value<string>("AbsentReasonId");

Equals will return falses as soon as the first mismatch is encountered. Contains or IndexOf on the other hand have to scan the entire string since Unknown may appear in the middle or at the end of the string.

Migrate from Newtonsoft.Json to System.Text.Json, Json does case-insensitive property name matching by default. C# Copy. public class ExampleClass { public string String1 { get; set; } public string Returns a JObject or JArray for complex values in the JSON payload. c# - array - LINQ Distinct operator, ignore case? c# string array contains case insensitive (3)

JSON.NET – CASE INSENSITIVE DICTIONARY, I needed the dictionary to be case insensitive when comparing string-keys, C#. new Dictionary<string, T>(StringComparer.OrdinalIgnoreCase); Debug("​Reader's token type is null"); return null; } var jsonObject = JObject. Since the case didn't match, this JSON property is an extra and becomes a key-value pair in the dictionary. DatesAvailable: 8/1/2019 12:00:00 AM -07:00 8/2/2019 12:00:00 AM -07:00: Extra property from the JSON becomes a key-value pair, with an array as the value object. SummaryWords: Cool Windy Humid

Working With JSON String In C#, In this article you will learn how to work with C# JSON string. key name must match with the class property name and matching is case insensitive. JObject class has parse method; it parses the JSON string and converts it  If you stumbled upon this post it probably means that you’re trying to use the String.Contains method in a C# application in a case-insensitive way: as a matter of fact, there isn’t a built-in overload method accepting a StringComparison options object, hence is not possible to do that.

Newtonsoft's CASE Insensitive selecting path to a jason token, DeepEquals(s2, s3)); // false JObject o1 = new JObject { { "Integer", 12345 } new JArray(1, 2) } }; JObject o2 = new JObject { { "Integer", 12345 }, { "String",  You can copy an array variable into another target array variable. In such case, both the target and source point to the same memory location − int [] marks = new int[] { 99, 98, 92, 97, 95}; int[] score = marks; When you create an array, C# compiler implicitly initializes each array element to a default value depending on the array type.

Comments
  • Shouldn't you use Equals instead of Contains?
  • Thank for pointing. is there any plus using Equals
  • Of course, it's faster because it doesn't have to scan the entire string before it decides there's a mismatch. It will return false once it finds the first mismatch. Contains will match strings that contain Unknown anywhere in them, eg this is not unknown.