JamesNK/Newtonsoft.Json · error · JsonException

Property '{0}' not valid on {1}.

Error message

Property '{0}' not valid on {1}.

What it means

Thrown during JSONPath evaluation when a property/field filter tries to access a named property on a token that is not a JObject, and ErrorWhenNoMatch is true. The FieldFilter can only read properties off objects, so encountering a JArray, JValue, or JProperty triggers the error reporting the property name and the actual token type.

Source

Thrown at Src/Newtonsoft.Json/Linq/JsonPath/FieldFilter.cs:47

                        }
                        else if (settings?.ErrorWhenNoMatch ?? false)
                        {
                            throw new JsonException("Property '{0}' does not exist on JObject.".FormatWith(CultureInfo.InvariantCulture, Name));
                        }
                    }
                    else
                    {
                        foreach (KeyValuePair<string, JToken?> p in o)
                        {
                            yield return p.Value!;
                        }
                    }
                }
                else
                {
                    if (settings?.ErrorWhenNoMatch ?? false)
                    {
                        throw new JsonException("Property '{0}' not valid on {1}.".FormatWith(CultureInfo.InvariantCulture, Name ?? "*", t.GetType().Name));
                    }
                }
            }
        }
    }
}

View on GitHub (pinned to 4f73e74372)

Solutions

  1. Insert an array index or wildcard between the array and the property, e.g. $.data[0].name or $.data[*].name.
  2. Verify the token type at each path segment before drilling into a property.
  3. Drop errorWhenNoMatch so a type mismatch yields zero results instead of throwing.

Example fix

// before
token.SelectTokens("$.data.name", errorWhenNoMatch: true); // data is a JArray

// after
token.SelectTokens("$.data[*].name", errorWhenNoMatch: false);
Defensive patterns

Strategy: type-guard

Validate before calling

var node = token["data"];
if (node is JObject obj)
{
    var name = obj["name"];
}

Type guard

static bool IsJsonObject(JToken? t) => t is JObject;

Prevention

When it happens

Trigger: Calling SelectTokens("$.data.name", errorWhenNoMatch: true) where 'data' is a JArray rather than a JObject. FieldFilter.cs:45-48 fires in the else branch (token is not JObject).

Common situations: Schema drift where an object field became an array; treating a list element as an object without first indexing into it; chaining a property access directly after a wildcard that resolves to a scalar.

Related errors


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