{"record":{"id":"8404e6cc6601fe10","repo":"JamesNK/Newtonsoft.Json","slug":"set-jarray-values-with-invalid-key-value-0-int","errorCode":null,"errorMessage":"Set JArray values with invalid key value: {0}. Int32 array index expected.","messagePattern":"Set JArray values with invalid key value: (.+?)\\. Int32 array index expected\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/JArray.cs","lineNumber":279,"sourceCode":"                }\n\n            }\n            set\n            {\n                ValidationUtils.ArgumentNotNull(key, nameof(key));\n\n                switch (key)\n                {\n                    case int intKey:\n                        SetItem(intKey, value);\n                        return;\n#if NET6_0_OR_GREATER\n                    case Index indexKey:\n                        SetItem(indexKey.GetOffset(Count), value);\n                        return;\n#endif\n                    default:\n                        throw new ArgumentException(\"Set JArray values with invalid key value: {0}. Int32 array index expected.\".FormatWith(CultureInfo.InvariantCulture, MiscellaneousUtils.ToString(key)));\n                }\n            }\n        }\n\n        /// <summary>\n        /// Gets or sets the <see cref=\"Newtonsoft.Json.Linq.JToken\"/> at the specified index.\n        /// </summary>\n        /// <value></value>\n        public JToken this[int index]\n        {\n            get => GetItem(index);\n            set => SetItem(index, value);\n        }\n\n        internal override int IndexOfItem(JToken? item)\n        {\n            if (item == null)\n            {","sourceCodeStart":261,"sourceCodeEnd":297,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JArray.cs#L261-L297","documentation":"Thrown by the JArray[object key] setter when assigning through the object-key indexer with a key that is neither int nor System.Index. The set path mirrors the get path and requires an integer position.","triggerScenarios":"Assigning myArray[\"name\"] = token, or using a boxed long/double as the index on the setter side.","commonSituations":"Building or mutating a JArray with code ported from JObject; dynamic dispatch passing a non-int key.","solutions":["Assign via the int indexer: myArray[index] = value.","To append, call myArray.Add(value); to overwrite an existing slot use the integer position.","Validate the key type before assignment when the index comes from dynamic input."],"exampleFix":"// before\nmyArray[(object)keyStr] = new JValue(1);\n// after\nmyArray[0] = new JValue(1);","handlingStrategy":"type-guard","validationCode":"if (key is int i) { arr[i] = value; }\nelse throw new InvalidOperationException(\"JArray setter requires an int index.\");","typeGuard":"static bool IsArrayKey(object key) => key is int\n#if NET6_0_OR_GREATER\n    || key is System.Index\n#endif\n;","tryCatchPattern":"try { arr[key] = value; }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"Int32 array index expected\")) {\n    // convert key to int or use Add\n}","preventionTips":["Assign via arr[int] = value.","Use Add to append rather than assigning past the end.","Keep JArray mutation code typed to int indices."],"tags":["jarray","indexer","argument"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}