JamesNK/Newtonsoft.Json · error · ArgumentException

Requested value '{0}' was not found.

Error message

Requested value '{0}' was not found.

What it means

ParseEnum (EnumUtils.cs:326-371) tries, in order: exact resolved-name match, case-insensitive whole-string match, and flag-by-flag composition. If after all attempts no enum member/combination resolves, it throws ArgumentException at EnumUtils.cs:371 reporting the requested value.

Source

Thrown at Src/Newtonsoft.Json/Utilities/EnumUtils.cs:371

                // if no match found, attempt case insensitive search
                if (matchingIndex == null)
                {
                    matchingIndex = MatchName(value, enumNames, resolvedNames, valueIndex, valueSubstringLength, StringComparison.OrdinalIgnoreCase);
                }

                if (matchingIndex == null)
                {
                    // still can't find a match
                    // before we throw an error, check whether the entire string has a case insensitive match against resolve names
                    matchingIndex = FindIndexByName(resolvedNames, value, 0, value.Length, StringComparison.OrdinalIgnoreCase);
                    if (matchingIndex != null)
                    {
                        return Enum.ToObject(enumType, enumValues[matchingIndex.Value]);
                    }

                    // no match so error
                    throw new ArgumentException("Requested value '{0}' was not found.".FormatWith(CultureInfo.InvariantCulture, value));
                }

                result |= enumValues[matchingIndex.Value];

                // Move our pointer to the ending index to go again.
                valueIndex = endIndex + 1;
            }

            return Enum.ToObject(enumType, result);
        }

        private static int? MatchName(string value, string[] enumNames, string[] resolvedNames, int valueIndex, int valueSubstringLength, StringComparison comparison)
        {
            int? matchingIndex = FindIndexByName(resolvedNames, value, valueIndex, valueSubstringLength, comparison);
            if (matchingIndex == null)
            {
                matchingIndex = FindIndexByName(enumNames, value, valueIndex, valueSubstringLength, comparison);
            }

View on GitHub (pinned to 4f73e74372)

Solutions

  1. Correct the JSON value to a valid member name (respecting [EnumMember(Value=...)] and the naming strategy).
  2. Add the missing member, or an [EnumMember(Value="...")] alias mapping the external spelling to a member.
  3. Register a JsonConverter that maps unknown enum values to a designated default/Unknown member.
  4. For [Flags] enums, ensure the string is a comma-separated list of valid flag names.

Example fix

// before: JSON "status": "actve" -> no match -> throws
// after: fix spelling to "active", or add
[EnumMember(Value = "actve")] Active,
Defensive patterns

Strategy: try-catch

Validate before calling

// Pre-check the value resolves to a known member before parsing.
var known = Enum.GetNames(enumType).SelectMany(n => new[] { n }); // plus [EnumMember] values if present
if (!known.Any(k => string.Equals(k, value, StringComparison.OrdinalIgnoreCase)))
    return default; // or map to an Unknown member

Type guard

static bool IsKnownEnumValue(Type enumType, string value) => Enum.GetNames(enumType).Any(n => string.Equals(n, value, StringComparison.OrdinalIgnoreCase));

Try / catch

try { return EnumUtils.ParseEnum(enumType, null, value, false); } catch (ArgumentException ex) when (ex.Message.Contains("was not found")) { return default; }

Prevention

When it happens

Trigger: An enum string that does not correspond to any defined member or valid flag combination — typo, removed/renamed member, or a value from a newer version of the enum.

Common situations: Version skew (producer emits a member the consumer's enum doesn't define); typos in JSON; unexpected values from third-party APIs; renamed enum members without [EnumMember] aliases.

Related errors


AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07). Data as JSON: /api/errors/14d0a6e2e08ec14c. Report an issue: GitHub.