Render empty list results as [] rather than null - #6
Open
waldyrious wants to merge 1 commit into
Open
waldyrious wants to merge 1 commit into
waldyrious wants to merge 1 commit into
Conversation
When a list query doesn't match any results, the JSON output includes `"items": null` instead of an empty list. Anything that reads the list then has to check for that first: `jq '.items[]'` stops with "Cannot iterate over null (null)", so scripts have to use safeguards like `(.items // [])[]` instead. When outputting in YAML format, the output instead shows `items: []`. This patch makes the JSON output match both the YAML and the docs.
Author
|
@anatolyrr I believe you may need to manually approve the CI workflow run. |
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 query returns no results, the JSON output has
"items": nullinstead of an empty list. This means that anything reading the list has to handle that case too. For example,jq '.items[]'stops withCannot iterate over null (null), so scripts have to write(.items // [])[]instead.This issue actually happens with the very
jqexample provided in the README: that command fails with the error mentioned above if the filter doesn't match anything.For contrast, the same query already gives
items: []in YAML:The reason for this discrepancy is that
Itemson theEnvelopehas no value at all when no results get appended to it, and the two encoders (JSON vs. YAML) render that case differently. Setting it to an empty list when the envelope is built fixes the JSON side, so both formats agree.A new test is added, which fails in the current main branch (with
"items":null) and passes with this change;make testis green, and I checked the patched binary against timestripe.com to confirm that--jsonnow gives[]while--yamlis unchanged.