Skip to content
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

Filter inputs away #5

Closed
stroan opened this issue Oct 3, 2012 · 2 comments
Closed

Filter inputs away #5

stroan opened this issue Oct 3, 2012 · 2 comments

Comments

@stroan
Copy link
Contributor

stroan commented Oct 3, 2012

I might well be reading the docs wrong, but I don't think there's a way to perform the following operation. Given the input

{"foo": "bar", "baz": 10}
{"foo": "blap", "baz": 11}
{"foo": "bar", "baz": 12}

Get only the objects where .foo == "bar", resulting in the output:

{"foo": "bar", "baz": 10}
{"foo": "bar", "baz": 12}

Currently the syntax for conditionals, requires an else case. If this were changed to allow only an if case and to not output a value if the case that doesn't match then the program which supplies the above could be:

./jq 'if .foo == "bar" then . end'

My current work around involves using the else case to output a sentinel value which I grep away before piping back into jq again.

@stedolan
Copy link
Contributor

stedolan commented Oct 3, 2012

./jq 'if .foo == "bar" then empty end'

would do what you want. empty is a builtin that produces zero results. You can in theory implement it yourself with something like [][] (each element of an empty array).

You should probably use the (sadly undocumented) builtin function select instead:

./jq 'select(.foo == "bar")'

I intentionally didn't implement if-without-else, since I'm not sure whether the best default for "else" behaviour is produce-no-output or dont-change-the-input (that is, whether the implicit else block should be . or empty). This sounds like a vote for empty.

@stroan
Copy link
Contributor Author

stroan commented Oct 3, 2012

Ah. Brilliant. I don't think there's any need for if-without-else in the presence of select and empty. Closing this ticket because there's no issue with jq, just a gap in the docs. Cheers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants