{"record":{"id":"eee0c0445c653278","repo":"JamesNK/Newtonsoft.Json","slug":"accessed-jarray-values-with-invalid-key-value-0","errorCode":null,"errorMessage":"Accessed JArray values with invalid key value: {0}. Int32 array index expected.","messagePattern":"Accessed 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":260,"sourceCode":"        /// Gets the <see cref=\"JToken\"/> with the specified key.\n        /// </summary>\n        /// <value>The <see cref=\"JToken\"/> with the specified key.</value>\n        public override JToken? this[object key]\n        {\n            get\n            {\n                ValidationUtils.ArgumentNotNull(key, nameof(key));\n\n                switch (key)\n                {\n                    case int intKey:\n                        return GetItem(intKey);\n#if NET6_0_OR_GREATER\n                    case Index indexKey:\n                        return GetItem(indexKey.GetOffset(Count));\n#endif\n                    default:\n                        throw new ArgumentException(\"Accessed JArray values with invalid key value: {0}. Int32 array index expected.\".FormatWith(CultureInfo.InvariantCulture, MiscellaneousUtils.ToString(key)));\n                }\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:","sourceCodeStart":242,"sourceCodeEnd":278,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JArray.cs#L242-L278","documentation":"Thrown by the JArray[object key] getter when the key is neither an int nor (on .NET 6+) a System.Index. JArray is positional, so only integer indices resolve an element; any other key type is invalid.","triggerScenarios":"Indexing with a string (arr[\"name\"]), a long, a double, or any non-int object via the object-key indexer.","commonSituations":"Code written for JObject (string keys) reused on a JArray; passing a dynamic/boxed value whose runtime type is long or byte rather than int.","solutions":["Use the strongly-typed int indexer: arr[intIndex].","If you need lookup by content, use LINQ (arr.First(t => ...)) or the First/Last properties.","If a string key was intended, parse the JSON as JObject instead of JArray."],"exampleFix":"// before\nJToken v = myArray[(object)\"id\"];\n// after\nJToken v = myArray[0];","handlingStrategy":"type-guard","validationCode":"if (key is int i) { JToken v = arr[i]; }\nelse throw new InvalidOperationException(\"JArray 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 { var v = arr[key]; }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"Int32 array index expected\")) {\n    // resolve via LINQ or convert key to int\n}","preventionTips":["Prefer the int indexer arr[int] over the object indexer.","Don't reuse JObject string-key code on JArray.","When key arrives via dynamic, coerce to int before indexing."],"tags":["jarray","indexer","argument"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}