JamesNK/Newtonsoft.Json · error · JsonException
Invalid JSON schema type: {0}
Error message
Invalid JSON schema type: {0} What it means
Thrown by JsonSchemaBuilder.MapType when the 'type' field of a JSON Schema contains a string that is not in the recognized mapping (object, array, string, number, integer, boolean, null, any). The builder cannot map the token to a JsonSchemaType enum value.
Source
Thrown at Src/Newtonsoft.Json/Schema/JsonSchemaBuilder.cs:485
throw JsonException.Create(typeToken, typeToken.Path, "Expected JSON schema type string token, got {0}.".FormatWith(CultureInfo.InvariantCulture, token.Type));
}
type = type | MapType((string)typeToken);
}
return type;
case JTokenType.String:
return MapType((string)token);
default:
throw JsonException.Create(token, token.Path, "Expected array or JSON schema type string token, got {0}.".FormatWith(CultureInfo.InvariantCulture, token.Type));
}
}
internal static JsonSchemaType MapType(string type)
{
if (!JsonSchemaConstants.JsonSchemaTypeMapping.TryGetValue(type, out JsonSchemaType mappedType))
{
throw new JsonException("Invalid JSON schema type: {0}".FormatWith(CultureInfo.InvariantCulture, type));
}
return mappedType;
}
internal static string MapType(JsonSchemaType type)
{
return JsonSchemaConstants.JsonSchemaTypeMapping.Single(kv => kv.Value == type).Key;
}
}
}View on GitHub (pinned to 4f73e74372)
Solutions
- Use one of the valid JSON Schema type strings: object, array, string, number, integer, boolean, null, any.
- Map language types to schema types before emitting (e.g. int/double -> number/integer).
- Upgrade to the separate Newtonsoft.Json.Schema package if you need newer draft type semantics.
Example fix
// before
JsonSchema.Parse("{\"type\":\"double\"}");
// after
JsonSchema.Parse("{\"type\":\"number\"}"); Defensive patterns
Strategy: validation
Validate before calling
static readonly HashSet<string> ValidTypes = new()
{ "object","array","string","number","integer","boolean","null","any" };
static bool IsValidType(string type) => ValidTypes.Contains(type); Try / catch
try { JsonSchema.Parse(json); }
catch (JsonException ex) when (ex.Message.StartsWith("Invalid JSON schema type"))
{ /* map language type names to schema type names before parsing */ } Prevention
- Use JSON Schema type names (number, integer, boolean), not language types (double, int, bool).
- Validate the 'type' field against the allowed set.
- Use Newtonsoft.Json.Schema package for newer drafts.
When it happens
Trigger: A schema with an unrecognized type value. Examples: JsonSchema.Parse("{\"type\":\"double\"}") (should be 'number'), "{\"type\":\"int\"}" (should be 'integer'), "{\"type\":\"datetime\"}", or a typo like 'strnig'.
Common situations: Using draft-4+ type names not supported by the legacy draft-3 builder; typos; mapping from a different schema dialect; auto-generated schemas using language type names (int, long, double) instead of JSON Schema type names.
Understand the failure class
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- Property {0} has already been defined in schema.
- Could not resolve schema reference '{0}'.
- Unresolved circular reference for type '{0}'. Explicitly def
- Unexpected contract type: {0}
- Newtonsoft.Json serialization is not compatible with trimmin
AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07).
Data as JSON: /api/errors/d1df1c3f681ab052.
Report an issue: GitHub.