{"record":{"id":"5489440f363fecfb","repo":"antonmedv/fx","slug":"trailing-comma-is-not-allowed-in-strict-mode","errorCode":null,"errorMessage":"Trailing comma is not allowed in strict mode","messagePattern":"Trailing comma is not allowed in strict mode","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/jsonx/json.go","lineNumber":369,"sourceCode":"\t\tvalue := p.parseValue(false)\n\t\tvalue.Key = keyBytes\n\t\tvalue.Parent = object\n\t\tp.depth--\n\n\t\tobject.Append(value)\n\t\tobject.Size += 1\n\n\t\tp.skipWhitespace()\n\n\t\tcommaPos := p.end\n\t\tif p.char == ',' {\n\t\t\tobject.End.Comma = true\n\t\t\tp.next()\n\t\t\tp.skipWhitespace()\n\t\t\tif p.char == '}' {\n\t\t\t\tif p.strict {\n\t\t\t\t\tp.set(commaPos)\n\t\t\t\t\tpanic(\"Trailing comma is not allowed in strict mode\")\n\t\t\t\t}\n\t\t\t\tobject.End.Comma = false\n\t\t\t} else {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t}\n\n\t\tif p.char == '}' {\n\t\t\tcloseBracket := &Node{\n\t\t\t\tKind:       Object,\n\t\t\t\tDepth:      p.depth,\n\t\t\t\tLineNumber: p.lineNumberPlusPlus(),\n\t\t\t}\n\t\t\tcloseBracket.Value = curlyBracketClose\n\t\t\tcloseBracket.Parent = object\n\t\t\tcloseBracket.Index = -1\n\t\t\tobject.Append(closeBracket)\n\t\t\tp.next()","sourceCodeStart":351,"sourceCodeEnd":387,"githubUrl":"https://github.com/antonmedv/fx/blob/4f31cd3a0c5d66f1b4290a2719bab14a5cee8ebe/internal/jsonx/json.go#L351-L387","documentation":"The parser tolerates trailing commas in non-strict mode, but in strict mode (JsonParser created with strict=true) a comma immediately before a closing '}' is rejected. Before panicking, it rewinds the parser position back to the comma (p.set(commaPos)) so the reported location points at the trailing comma. Strict mode follows the JSON RFC, which forbids trailing commas.","triggerScenarios":"Parsing {\"a\":1,} or {\"a\":1, } with strict mode enabled (e.g. ParseStrict-style APIs or parser configured with strict: true).","commonSituations":"JSON copied from JavaScript source or ESLint/Prettier-formatted code where trailing commas are stylistic; build tools emitting JS-flavored JSON; hand-edited config where an entry was deleted leaving a dangling comma.","solutions":["Remove the trailing comma before the closing brace: {\"a\":1,} -> {\"a\":1}","If trailing commas are acceptable for your use case, parse in non-strict mode instead of strict mode","Normalize the input before parsing (strip ',}' / ',]' sequences)"],"exampleFix":"// before (strict parse)\n{\"a\": 1, \"b\": 2,}\n\n// after\n{\"a\": 1, \"b\": 2}","handlingStrategy":"validation","validationCode":"func stripTrailingCommas(src []byte) []byte {\n\t// lenient preprocessor: remove ',}' and ',]' outside strings (strict-mode input)\n\tout := make([]byte, 0, len(src))\n\tinStr, esc := false, false\n\tfor i := 0; i < len(src); i++ {\n\t\tc := src[i]\n\t\tif esc { esc = false; out = append(out, c); continue }\n\t\tif c == '\\\\' && inStr { esc = true; out = append(out, c); continue }\n\t\tif c == '\"' { inStr = !inStr; out = append(out, c); continue }\n\t\tif !inStr && c == ',' {\n\t\t\tj := i + 1\n\t\t\tfor j < len(src) && (src[j]==' '||src[j]=='\\t'||src[j]=='\\n'||src[j]=='\\r') { j++ }\n\t\t\tif j < len(src) && (src[j] == '}' || src[j] == ']') { continue }\n\t\t}\n\t\tout = append(out, c)\n\t}\n\treturn out\n}","typeGuard":null,"tryCatchPattern":"func parseStrictLenient(p *jsonx.JsonParser, data []byte) (root *jsonx.Node, err error) {\n\tdefer func() {\n\t\tif r := recover(); r != nil {\n\t\t\tif strings.Contains(fmt.Sprint(r), \"Trailing comma\") {\n\t\t\t\t// fallback: retry after stripping trailing commas\n\t\t\t\tdefer func() { recover() }()\n\t\t\t\troot = jsonx.NewParser(jsonx.WithStrict(false)).Parse(stripTrailingCommas(data))\n\t\t\t\treturn\n\t\t\t}\n\t\t\terr = fmt.Errorf(\"json parse failed: %v\", r)\n\t\t}\n\t}()\n\troot = p.Parse(data)\n\treturn\n}","preventionTips":["Decide strict vs lenient mode once at app level and document it","If consuming JS-derived JSON, either disable strict mode or strip trailing commas first","Configure linters/prettier on JSON files with \"trailingComma\": \"none\"","Strip trailing commas in a preprocessing step before strict parsing"],"tags":["json","strict-mode","trailing-comma"],"backgroundTag":"json-trailing-comma","analyzedSha":"4f31cd3a0c5d66f1b4290a2719bab14a5cee8ebe","analyzedAt":"2026-09-02T02:17:47.344Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}