{"record":{"id":"2b995fd4a8affc2c","repo":"Tencent/WeKnora","slug":"failed-to-parse-json-w","errorCode":null,"errorMessage":"failed to parse JSON: %w","messagePattern":"failed to parse JSON: %w","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/infrastructure/docparser/json_converter.go","lineNumber":40,"sourceCode":"// Key properties:\n//   - Every output chunk is a **valid JSON object** (not a fragment).\n//   - Nested paths from root to leaf are **fully preserved** in each chunk.\n//   - Arrays are converted to index-keyed dicts so the algorithm is uniform.\n//   - Small objects that fit within maxChunkSize are kept intact (not split).\n//   - The output is a series of fenced ```json code blocks separated by \\n\\n,\n//     which the downstream text chunker can split at block boundaries.\nfunc jsonToMarkdown(data []byte) (string, error) {\n\tdata = trimBOM(data)\n\tif len(data) == 0 {\n\t\treturn \"\", fmt.Errorf(\"empty JSON content\")\n\t}\n\tif !json.Valid(data) {\n\t\treturn \"\", fmt.Errorf(\"invalid JSON content\")\n\t}\n\n\tvar parsed interface{}\n\tif err := json.Unmarshal(data, &parsed); err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to parse JSON: %w\", err)\n\t}\n\n\t// Normalize: convert top-level arrays to index-keyed dicts\n\tnormalized := listToDictPreprocess(parsed)\n\n\t// If the whole thing fits in one chunk, just format it\n\twholeSize := jsonSize(normalized)\n\tif wholeSize <= defaultJSONChunkSize {\n\t\tformatted := formatValue(normalized)\n\t\treturn wrapCodeBlock(formatted), nil\n\t}\n\n\t// Recursive split\n\tchunks := recursiveJSONSplit(normalized, nil, nil)\n\n\t// Convert each chunk dict to a fenced code block\n\tblocks := make([]string, 0, len(chunks))\n\tfor _, chunk := range chunks {","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/Tencent/WeKnora/blob/988cbb03305e055d8ebb7d46d9ac6cc0803cd074/internal/infrastructure/docparser/json_converter.go#L22-L58","documentation":"Even when json.Valid passes, json.Unmarshal can still fail (e.g. numbers out of range for interface{} decoding); jsonToMarkdown wraps that underlying error as \"failed to parse JSON: %w\". It means the content looked like JSON but could not be materialized into Go values.","triggerScenarios":"Read (json_converter) called with syntactically valid JSON that json.Unmarshal rejects — most commonly numeric literals outside float64 range (huge integers/exponents) or exotic literals that pass a shallow validity check but fail decoding.","commonSituations":"Machine-generated JSON with 64-bit+ integer IDs serialized as bare numbers; scientific-notation numbers beyond float64; content valid per json.Valid but rejected during decode due to Go's number handling.","solutions":["Inspect the wrapped cause in the error message (%w chain) to see the exact offset/reason reported by encoding/json.","Serialize very large integer identifiers as JSON strings instead of bare numbers.","Re-export the JSON with a serializer that emits float64-safe numbers (e.g. cap precision or use strings for big values).","If you control parsing, use json.Decoder with UseNumber() — otherwise this library path cannot be changed, so fix the input data."],"exampleFix":"// before\n{\"id\": 922337203685477580712345}\n// after\n{\"id\": \"922337203685477580712345\"}","handlingStrategy":"try-catch","validationCode":"var probe interface{}\nif err := json.Unmarshal(data, &probe); err != nil {\n    return fmt.Errorf(\"content will fail conversion: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"var jsonNumErr *json.UnmarshalTypeError\nmd, err := converter.Read(ctx, req)\nif err != nil {\n    if errors.As(err, &jsonNumErr) || strings.Contains(err.Error(), \"failed to parse JSON\") {\n        log.Errorf(\"JSON decode failed at offset %v: %v\", jsonNumErr, err)\n        return fmt.Errorf(\"fix the JSON value reported in the wrapped error and retry\")\n    }\n    return err\n}","preventionTips":["Serialize large integers/IDs as JSON strings to stay within float64 range.","Always surface the full wrapped error chain (%w) to see encoding/json's root cause.","Round-trip test your exports through Go's encoding/json before shipping them to the parser."],"tags":["json","unmarshal","encoding"],"backgroundTag":"json-parse-failed","analyzedSha":"988cbb03305e055d8ebb7d46d9ac6cc0803cd074","analyzedAt":"2026-09-02T14:41:08.344Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}