Skip to content

fix: jq filter returns null for empty array responses#461

Merged
rspurgeon merged 2 commits intomainfrom
copilot/fix-jq-parser-empty-arrays
Feb 21, 2026
Merged

fix: jq filter returns null for empty array responses#461
rspurgeon merged 2 commits intomainfrom
copilot/fix-jq-parser-empty-arrays

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 21, 2026

When a list command returns zero items, applying a jq filter (e.g., --jq '.') outputs null instead of []. Without --jq, the output is correctly [].

Root cause: Go's json.Marshal encodes nil slices as null, not []. SDK list responses use nil slices for empty results. segmentio/cli's printer handles nil typed slices as [] directly, bypassing json.Marshal — so the discrepancy only surfaces through the jq code path.

Changes

  • internal/cmd/output/jq/jq.go: Add marshalForJQ helper that uses reflection to detect nil slices and encode them as [] before jq processing. ApplyToRaw now calls this instead of json.Marshal directly.
func marshalForJQ(v any) ([]byte, error) {
    if v != nil {
        rv := reflect.ValueOf(v)
        if rv.Kind() == reflect.Slice && rv.IsNil() {
            return []byte("[]"), nil
        }
    }
    return json.Marshal(v)
}
  • internal/cmd/output/jq/jq_test.go: Add tests for nil-slice and empty-slice identity filter cases.
Original prompt

This section details on the original issue you should resolve

<issue_title>Bug: Embedded jq parser does not handle empty arrays as expected</issue_title>
<issue_description>Take the following example commands and the sample command data passed to jq for comparison. The identity expression . should echo an empty array.

└─❯❯ ./kongctl get apis --output json
[]

└─❯❯ ./kongctl get apis --output json --jq '.'
null

└─❯❯ echo "[]" | jq '.'
[]
```</issue_description>

<agent_instructions>Proceed with Option 1 From the triage report</agent_instructions>

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>


🔒 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.

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
Copilot AI requested a review from rspurgeon February 21, 2026 17:53
@rspurgeon rspurgeon marked this pull request as ready for review February 21, 2026 18:18
@rspurgeon rspurgeon requested a review from a team as a code owner February 21, 2026 18:18
Copilot AI review requested due to automatic review settings February 21, 2026 18:18
@rspurgeon rspurgeon requested a review from a team as a code owner February 21, 2026 18:18
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 marshalForJQ helper to encode top-level nil slices as [] prior to jq evaluation.
  • Update ApplyToRaw to use marshalForJQ instead of json.Marshal directly.
  • 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.

@rspurgeon rspurgeon merged commit 4ddff53 into main Feb 21, 2026
14 checks passed
@rspurgeon rspurgeon deleted the copilot/fix-jq-parser-empty-arrays branch February 21, 2026 18:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Embedded jq parser does not handle empty arrays as expected

3 participants