fix: jq filter returns null for empty array responses#461
Merged
Conversation
Co-authored-by: rspurgeon <10521262+rspurgeon@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix embedded jq parser handling of empty arrays
fix: jq filter returns null for empty array responses
Feb 21, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes jq-filtered output for list commands that return zero items by ensuring nil slices are marshaled as [] (instead of null) before jq processing, matching the non-jq JSON output behavior across the CLI.
Changes:
- Add a
marshalForJQhelper to encode top-level nil slices as[]prior to jq evaluation. - Update
ApplyToRawto usemarshalForJQinstead ofjson.Marshaldirectly. - Add unit tests covering identity jq filtering (
.) for both nil and empty slices.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| internal/cmd/output/jq/jq.go | Introduces marshalForJQ and routes jq preprocessing through it to normalize nil-slice JSON encoding. |
| internal/cmd/output/jq/jq_test.go | Adds regression tests ensuring --jq '.' returns [] for nil/empty slice inputs. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When a list command returns zero items, applying a jq filter (e.g.,
--jq '.') outputsnullinstead of[]. Without--jq, the output is correctly[].Root cause: Go's
json.Marshalencodes nil slices asnull, not[]. SDK list responses use nil slices for empty results.segmentio/cli's printer handles nil typed slices as[]directly, bypassingjson.Marshal— so the discrepancy only surfaces through the jq code path.Changes
internal/cmd/output/jq/jq.go: AddmarshalForJQhelper that uses reflection to detect nil slices and encode them as[]before jq processing.ApplyToRawnow calls this instead ofjson.Marshaldirectly.internal/cmd/output/jq/jq_test.go: Add tests for nil-slice and empty-slice identity filter cases.Original prompt
🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.