OrchardCMS/OrchardCore · error · FormatException

Can't use the non numeric key

Error message

Can't use the non numeric key '{child.Key}' inside an array.

What it means

ToJsonNode is the mirror case of the numeric-key error: once a level has started building a JsonArray (numeric keys), any subsequent non-numeric key at the same level cannot be represented, so FormatException is thrown naming the key.

Solutions

  1. Remove the non-numeric key from the array-shaped level, or restructure as an object with named keys.
  2. Keep arrays pure: keys must be exactly 0..count-1 with no extra named entries.
  3. Use GetDebugView() on IConfiguration to find which provider contributes the conflicting key.
  4. Bind to a typed model (List<T> or Dictionary<string,T>) instead of ToJsonNode if the shape is mixed.

Example fix

// before
"Items": { "0": "a", "1": "b", "Default": "a" }
// after
"Items": ["a", "b"], "Default": "a"
Defensive patterns

Strategy: validation

Validate before calling

var keys = configuration.GetChildren().Select(c => c.Key).ToList();
bool mixed = keys.Any(k => int.TryParse(k, out _)) && keys.Any(k => !int.TryParse(k, out _));
if (mixed) throw new FormatException("Config array level contains a non-numeric key.");

Type guard

bool IsObjectShaped(IConfiguration c) => c.GetChildren().All(ch => !int.TryParse(ch.Key, out _));

Try / catch

try { var node = configuration.ToJsonNode(); }
catch (FormatException ex) when (ex.Message.Contains("non numeric key")) { /* restructure keys or bind to Dictionary<string,T> */ }

Prevention

When it happens

Trigger: A configuration level contains numeric keys ('0','1') and then a named key, e.g. 'Items:0', 'Items:1', plus 'Items:Name' — the named key hits the jArray-not-null branch and throws.

Common situations: Mixing array syntax and object syntax in appsettings after a manual edit; migration scripts writing both 'Foo:0' and 'Foo:Default'; different providers (env vars + JSON) contributing conflicting keys at the same path.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


AI-assisted analysis of OrchardCMS/OrchardCore@4306c0717f (2026-09-13). Data as JSON: /api/errors/5b1d119d278ee6ff. Report an issue: GitHub.

Appendix: source

Thrown at src/OrchardCore/OrchardCore.Abstractions/Shell/Configuration/Internal/ConfigurationExtensions.cs:58

                    {
                        jArray.Add(null);
                    }
                }

                if (child.GetChildren().Any())
                {
                    jArray.Add(ToJsonNode(child));
                }
                else
                {
                    jArray.Add(child.Value);
                }
            }
            else
            {
                if (jArray is not null)
                {
                    throw new FormatException($"Can't use the non numeric key '{child.Key}' inside an array.");
                }

                jObject ??= [];
                if (child.GetChildren().Any())
                {
                    jObject.Add(child.Key, ToJsonNode(child));
                }
                else
                {
                    jObject.Add(child.Key, child.Value);
                }
            }
        }

        return jArray as JsonNode ?? jObject ?? [];
    }

    public static JsonObject ToJsonObject(this IDictionary<string, string> configurationData)

View on GitHub (pinned to 4306c0717f)