Skip to content

Commit afac1d7

Browse files
committed
Fix HTML-escaping of & < > in JSON output
Go's json.Marshal escapes & to \u0026 by default (HTML-safe mode). Switch to json.NewEncoder with SetEscapeHTML(false) so URLs in pagination and other fields render with literal & characters.
1 parent cf82131 commit afac1d7

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

pkg/output/output.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package output
22

33
import (
4+
"bytes"
45
"encoding/json"
56
"fmt"
67
"io"
@@ -28,10 +29,23 @@ func shouldColor() bool {
2829
return term.IsTerminal(int(os.Stdout.Fd()))
2930
}
3031

32+
// marshalJSON encodes v to indented JSON without HTML-escaping & < >.
33+
func marshalJSON(v any) ([]byte, error) {
34+
var buf bytes.Buffer
35+
enc := json.NewEncoder(&buf)
36+
enc.SetEscapeHTML(false)
37+
enc.SetIndent("", " ")
38+
if err := enc.Encode(v); err != nil {
39+
return nil, err
40+
}
41+
// Encoder always appends a trailing newline; trim it for consistent handling.
42+
return bytes.TrimRight(buf.Bytes(), "\n"), nil
43+
}
44+
3145
// PrintJSON prints a value to stdout as pretty-printed JSON.
3246
// Colorized when stdout is a TTY and NO_COLOR is not set.
3347
func PrintJSON(v any) error {
34-
data, err := json.MarshalIndent(v, "", " ")
48+
data, err := marshalJSON(v)
3549
if err != nil {
3650
return err
3751
}
@@ -67,7 +81,7 @@ func PrintJQValue(v any, w io.Writer) error {
6781
_, err := fmt.Fprintln(w, val)
6882
return err
6983
default:
70-
data, err := json.MarshalIndent(v, "", " ")
84+
data, err := marshalJSON(v)
7185
if err != nil {
7286
return err
7387
}

0 commit comments

Comments
 (0)