-
Notifications
You must be signed in to change notification settings - Fork 1
fix: use SDK RawJSON() for -o json output to avoid zero values #69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
When using -o json, the CLI was re-marshaling SDK response structs with json.MarshalIndent(), which included Go zero values for fields not present in the API response (e.g., deleted_at, persistence, profile, viewport). This change uses the SDK's RawJSON() method which preserves the original API response, only outputting fields that were actually returned. Adds PrintPrettyJSON and PrintPrettyJSONSlice helpers in pkg/util/json.go that pretty-print the raw JSON from SDK response types.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice cleanup overall — switching -o json to the SDK’s RawJSON() avoids the zero-value field noise, and the call sites read a lot cleaner.
Two small things in the new helpers:
PrintPrettyJSONcurrently prints raw JSON onjson.Indentfailure and then returnsnil. Since callers already propagate the error, returning the indent error would make unexpected/invalidRawJSON()failures easier to notice.PrintPrettyJSONSlicedouble-indents elements on the success path (prefixing injson.Indentand then adding another two spaces). Dropping the extra prefix keeps output consistent with the fallback branch and typicalMarshalIndentformatting.
Everything else in the diff looks solid, and the test update to populate RawJSON() via json.Unmarshal makes sense.
| if err := json.Indent(&buf, []byte(raw), "", " "); err != nil { | ||
| // If indentation fails, print the raw JSON as-is | ||
| fmt.Println(raw) | ||
| return nil | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PrintPrettyJSON currently swallows json.Indent errors and always returns nil, which makes it harder to detect when RawJSON() is unexpectedly empty/invalid. Since callers already return util.PrintPrettyJSON(...), it might be better to propagate the error.
| if err := json.Indent(&buf, []byte(raw), "", " "); err != nil { | |
| // If indentation fails, print the raw JSON as-is | |
| fmt.Println(raw) | |
| return nil | |
| } | |
| var buf bytes.Buffer | |
| if err := json.Indent(&buf, []byte(raw), "", " "); err != nil { | |
| return err | |
| } | |
| fmt.Println(buf.String()) | |
| return nil |
| if err := json.Indent(&elemBuf, []byte(raw), " ", " "); err != nil { | ||
| // Fallback to raw if indentation fails | ||
| buf.WriteString(" ") | ||
| buf.WriteString(raw) | ||
| } else { | ||
| buf.WriteString(" ") | ||
| buf.Write(elemBuf.Bytes()) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In PrintPrettyJSONSlice, the else path double-indents each element: json.Indent(..., prefix=" ") already prefixes the first line, and then you add another buf.WriteString(" ") before writing elemBuf. This makes the indent differ between the success and fallback branches.
| if err := json.Indent(&elemBuf, []byte(raw), " ", " "); err != nil { | |
| // Fallback to raw if indentation fails | |
| buf.WriteString(" ") | |
| buf.WriteString(raw) | |
| } else { | |
| buf.WriteString(" ") | |
| buf.Write(elemBuf.Bytes()) | |
| } | |
| var elemBuf bytes.Buffer | |
| if err := json.Indent(&elemBuf, []byte(raw), " ", " "); err != nil { | |
| // Fallback to raw if indentation fails | |
| buf.WriteString(" ") | |
| buf.WriteString(raw) | |
| } else { | |
| buf.Write(elemBuf.Bytes()) | |
| } |
- Return json.Indent error in PrintPrettyJSON instead of swallowing it - Fix double-indentation in PrintPrettyJSONSlice by removing extra prefix - Use json.Marshal for liveViewUrl to ensure valid JSON escaping
…Slice json.Indent only adds the prefix after newlines, not before the first character. This adds the 2-space indent before writing each element to ensure proper JSON array formatting.
Summary
-o json, the CLI was re-marshaling SDK response structs withjson.MarshalIndent(), which included Go zero values for fields not present in the API response (e.g.,deleted_at,persistence,profile,viewport)RawJSON()method which preserves the original API response, only outputting fields that were actually returnedPrintPrettyJSONandPrintPrettyJSONSlicehelpers inpkg/util/json.gothat pretty-print the raw JSON from SDK response typesBefore
{ "cdp_ws_url": "wss://...", "created_at": "2026-01-12T02:41:42.658978979Z", "session_id": "wvmx4yt5afram2xobclje52x", "timeout_seconds": 60, "deleted_at": "0001-01-01T00:00:00Z", "kiosk_mode": false, "persistence": {"id": ""}, "profile": {"id": "", "created_at": "0001-01-01T00:00:00Z", ...}, "viewport": {"height": 0, "width": 0, "refresh_rate": 0} }After
{ "browser_live_view_url": "https://...", "cdp_ws_url": "wss://...", "created_at": "2026-01-12T02:49:16.736473188Z", "headless": false, "session_id": "tpuq0sl28j5d8par13mqo79d", "stealth": false, "timeout_seconds": 60 }Test plan
make test)kernel browsers create --output json- no zero-value fieldskernel browsers list --output json- no zero-value fieldsNote
Standardizes
--output jsonto pretty-print the original API response using SDKRawJSON()instead of re-marshaling Go structs.pkg/util/json.gowithPrintPrettyJSONandPrintPrettyJSONSlicehelpers that indent raw SDK JSONapp,deploy history,browser-pools,browsersincl. replays/process/fs,extensions,proxies) to use the new helpers; consistent empty slice handlingbrowsers viewto emit{ "liveViewUrl": ... }with proper JSON escapingbrowsers_test.go) to validate pretty JSON output using populatedRawJSON()Written by Cursor Bugbot for commit beccbd7. This will update automatically on new commits. Configure here.