JamesNK/Newtonsoft.Json · error · JsonException

Property {0} has already been defined in schema.

Error message

Property {0} has already been defined in schema.

What it means

Thrown by JsonSchemaBuilder.ProcessProperties when the same property name appears more than once in a single 'properties' object of a JSON Schema document. JSON Schema (draft-3, which this builder targets) forbids duplicate keys in the properties map.

Source

Thrown at Src/Newtonsoft.Json/Schema/JsonSchemaBuilder.cs:424

            {
                CurrentSchema.AdditionalItems = BuildSchema(token);
            }
        }

        private IDictionary<string, JsonSchema> ProcessProperties(JToken token)
        {
            IDictionary<string, JsonSchema> properties = new Dictionary<string, JsonSchema>();

            if (token.Type != JTokenType.Object)
            {
                throw JsonException.Create(token, token.Path, "Expected Object token while parsing schema properties, got {0}.".FormatWith(CultureInfo.InvariantCulture, token.Type));
            }

            foreach (JProperty propertyToken in token)
            {
                if (properties.ContainsKey(propertyToken.Name))
                {
                    throw new JsonException("Property {0} has already been defined in schema.".FormatWith(CultureInfo.InvariantCulture, propertyToken.Name));
                }

                properties.Add(propertyToken.Name, BuildSchema(propertyToken.Value));
            }

            return properties;
        }

        private void ProcessItems(JToken token)
        {
            CurrentSchema.Items = new List<JsonSchema>();

            switch (token.Type)
            {
                case JTokenType.Object:
                    CurrentSchema.Items.Add(BuildSchema(token));
                    CurrentSchema.PositionalItemsValidation = false;
                    break;

View on GitHub (pinned to 4f73e74372)

Solutions

  1. Remove the duplicate property entry so each name appears once.
  2. Deduplicate generated property maps before serializing the schema JSON.
  3. Validate schema documents with a strict JSON parser that rejects duplicate keys.

Example fix

// before
// {"properties":{"name":{},"name":{"type":"string"}}}
// after
// {"properties":{"name":{"type":"string"}}}
Defensive patterns

Strategy: validation

Validate before calling

static bool HasUniqueProperties(JObject schemaJson)
{
    var props = schemaJson["properties"] as JObject;
    if (props == null) return true;
    var seen = new HashSet<string>();
    foreach (var p in props.Properties())
        if (!seen.Add(p.Name)) return false;
    return true;
}

Try / catch

try { JsonSchema.Parse(json); }
catch (JsonException ex) when (ex.Message.StartsWith("Property") && ex.Message.Contains("already been defined"))
{ /* duplicate property name; dedupe the source */ }

Prevention

When it happens

Trigger: Parsing a schema JSON where the properties object lists a key twice. Example: JsonSchema.Parse("{\"properties\":{\"name\":{},\"name\":{}}}") — even though the JSON parser may deduplicate, the builder iterates JProperty tokens and detects the duplicate as stored/seen.

Common situations: Hand-authored schemas with copy/paste duplication; schema generation tools that merge property sets without dedup; JSON that was not validated for unique keys; concatenating schema fragments programmatically.

Related errors


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