{"record":{"id":"61943d9490a06369","repo":"vitessio/vitess","slug":"expected-or-in-array-got-q","errorCode":null,"errorMessage":"expected ',' or ']' in array, got %q","messagePattern":"expected ',' or '\\]' in array, got %q","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"go/mysql/json/marshal.go","lineNumber":323,"sourceCode":"\t}\n}\n\nfunc (w *sqlWriter) writeArray(depth int) error {\n\tw.buf.WriteString(\"JSON_ARRAY(\")\n\tfirst := true\n\tfor {\n\t\tw.skipWhitespace()\n\t\tif w.pos >= len(w.data) {\n\t\t\treturn errors.New(\"unexpected end of JSON input in array\")\n\t\t}\n\t\tif w.data[w.pos] == ']' {\n\t\t\tw.pos++\n\t\t\tw.buf.WriteByte(')')\n\t\t\treturn nil\n\t\t}\n\t\tif !first {\n\t\t\tif w.data[w.pos] != ',' {\n\t\t\t\treturn fmt.Errorf(\"expected ',' or ']' in array, got %q\", w.data[w.pos])\n\t\t\t}\n\t\t\tw.pos++\n\t\t\tw.buf.WriteString(\", \")\n\t\t}\n\t\tfirst = false\n\n\t\tif err := w.writeValue(false, depth+1); err != nil {\n\t\t\treturn err\n\t\t}\n\t}\n}\n\nfunc (w *sqlWriter) writeString(top bool) error {\n\tif top {\n\t\tw.buf.WriteString(\"CAST(JSON_QUOTE(\")\n\t}\n\tw.buf.WriteString(\"_utf8mb4\")\n\tif err := w.writeStringContent(); err != nil {","sourceCodeStart":305,"sourceCodeEnd":341,"githubUrl":"https://github.com/vitessio/vitess/blob/01a25a7d176f94613b8d59d799f438380a8760e4/go/mysql/json/marshal.go#L305-L341","documentation":"While serializing a JSON array into a JSON_ARRAY(...) SQL expression, the parser expects the next byte after an element to be a comma (element separator) or the closing bracket. If the byte at the current position is anything else, the array is structurally malformed and the writer aborts with this error, reporting the offending character. This prevents silently producing a wrong SQL expression from corrupted input.","triggerScenarios":"Calling the JSON-to-SQL writer with an array containing adjacent values without a comma (e.g. [1 2]), a stray character between elements (e.g. [1 ; 2]), a missing closing bracket followed by unexpected bytes ([1 2]] with the inner document truncated), or an object used where the array separator was expected ([{}} malformed).","commonSituations":"JSON produced by concatenating serialized elements without separators in custom logging or batching code; truncated documents from network reads or fixed-size buffers; hand-written JSON fixtures with typos; data migrated from a system that emits a non-standard serialization.","solutions":["Run json.Valid (or encoding/json Unmarshal) on the payload before writing; reject invalid documents at the boundary.","Inspect the character reported in the error to find the missing/misplaced comma in the array and fix the producer that emitted it.","Replace hand-rolled JSON construction with json.Marshal / json.Encoder so separators and brackets are always correct.","If truncation is the cause, read the full document (e.g. length-prefixed framing) before parsing instead of parsing partial buffers."],"exampleFix":"// before: elements concatenated without separators\ns := \"[\" + strings.Join(items, \" \") + \"]\"\n// after\nb, _ := json.Marshal(items)","handlingStrategy":"validation","validationCode":"if !json.Valid(input) {\n\treturn errors.New(\"payload is not valid JSON: array separator missing or misplaced\")\n}","typeGuard":null,"tryCatchPattern":"if err := writeJSONAsSQL(input); err != nil {\n\tif strings.Contains(err.Error(), \"expected ',' or ']' in array\") {\n\t\treturn reencodeWithStdLib(rawSource) // rebuild from original typed data\n\t}\n\treturn err\n}","preventionTips":["Serialize arrays with json.Marshal instead of joining strings","Never parse partially-read buffers; read the complete document first","Keep hand-written JSON fixtures covered by json.Valid tests","Reject non-standard serializations at ingestion time"],"tags":["json","parsing","syntax-error","mysql"],"backgroundTag":"invalid-json-syntax","analyzedSha":"01a25a7d176f94613b8d59d799f438380a8760e4","analyzedAt":"2026-09-01T17:28:30.605Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}