Tencent/WeKnora · error

json conversion failed: %w

Error message

json conversion failed: %w

What it means

The built-in document converter converts JSON files to Markdown via jsonToMarkdown. This error wraps failures converting the uploaded JSON — almost always invalid JSON syntax (trailing commas, comments, single quotes, truncated file, or non-JSON content with a .json extension).

Source

Thrown at internal/infrastructure/docparser/builtin_converter.go:70

	if ft == "" {
		ft = strings.TrimPrefix(strings.ToLower(filepath.Ext(req.FileName)), ".")
	}

	switch {
	case ft == "md" || ft == "markdown":
		return &types.ReadResult{MarkdownContent: string(req.FileContent)}, nil
	case ft == "txt" || ft == "text":
		return &types.ReadResult{MarkdownContent: string(req.FileContent)}, nil
	case ft == "csv":
		md, err := csvToMarkdown(req.FileContent)
		if err != nil {
			return nil, fmt.Errorf("csv conversion failed: %w", err)
		}
		return &types.ReadResult{MarkdownContent: md}, nil
	case ft == "json":
		md, err := jsonToMarkdown(req.FileContent)
		if err != nil {
			return nil, fmt.Errorf("json conversion failed: %w", err)
		}
		return &types.ReadResult{MarkdownContent: md}, nil
	case imageFormats[ft]:
		return imageToResult(req.FileName, req.FileContent), nil
	case audioFormats[ft]:
		return audioToResult(req.FileName, req.FileContent), nil
	default:
		return nil, fmt.Errorf("unsupported simple format: %s", ft)
	}
}

// imageToResult wraps a standalone image as a markdown image reference with
// the raw bytes in ImageRefs, matching Python ImageParser behaviour.
func imageToResult(fileName string, data []byte) *types.ReadResult {
	if fileName == "" {
		fileName = "image.png"
	}
	refPath := "images/" + fileName

View on GitHub (pinned to 988cbb0330)

Solutions

  1. Validate the file with a strict JSON parser (jq, python -m json.tool) and fix the reported syntax error.
  2. Re-upload the file; a truncated/corrupt download is a common cause.
  3. Convert JSON5/JSONC (comments, trailing commas) to strict JSON before upload.
  4. Check the file encoding is UTF-8 without BOM; re-save if needed.

Example fix

// before
{ name: 'Bob', age: 3, } // JSON5, invalid strict JSON
// after
{"name": "Bob", "age": 3}
Defensive patterns

Strategy: validation

Validate before calling

if !json.Valid(bytes.TrimPrefix(req.FileContent, []byte("\xef\xbb\xbf"))) {
    return fmt.Errorf("file is not valid JSON")
}

Type guard

func isStrictJSON(data []byte) bool {
    return json.Valid(bytes.TrimPrefix(data, []byte("\xef\xbb\xbf")))
}

Try / catch

res, err := reader.Read(ctx, req)
if err != nil && strings.Contains(err.Error(), "json conversion failed") {
    var syn *json.SyntaxError
    if errors.As(err, &syn) { log.Printf("JSON syntax error at offset %d", syn.Offset) }
}

Prevention

When it happens

Trigger: Calling Read with file type "json" where json.Unmarshal inside jsonToMarkdown fails: truncated download, BOM prefix, NaN/Infinity literals, duplicate handling errors, or content that isn't actually JSON.

Common situations: Files renamed to .json but containing JSON5/JSONC (comments) or log lines; truncated uploads; UTF-16 encoded exports; single-quoted JS object literals saved as .json.

Related errors


AI-assisted analysis of Tencent/WeKnora@988cbb0330 (2026-09-02). Data as JSON: /api/errors/156beed0db811a6f. Report an issue: GitHub.