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

Stricter checking of path expressions in 1.5 #921

Closed
jakirkham opened this issue Aug 21, 2015 · 6 comments
Closed

Stricter checking of path expressions in 1.5 #921

jakirkham opened this issue Aug 21, 2015 · 6 comments
Labels

Comments

@jakirkham
Copy link

I am seeing an error in 1.5 that was not present in 1.4. Here are some test files and sample output that demonstrate the issue ( https://gist.github.com/jakirkham/0d2f4708cad69e71b1f6 ). In short, I am running the following command on an empty iPython Notebook and seeing the error below.

$ jq '( .cells[] | select(.outputs) ) |= [] | (.cells[] | select(.execution_count)) |= null' Untitled.ipynb
jq: error (at Untitled.ipynb:34): Invalid path expression with result {"cell_type":"code","execu...
@dtolnay
Copy link
Member

dtolnay commented Aug 21, 2015

It was a bug in jq 1.4 that allowed this to work in the first place. It looks like the code you meant to write is:

jq '(.cells[].outputs) = [] | (.cells[].execution_count) = null'

Let me know if I misinterpreted your example. It would help to have the input you used as well.

@dtolnay dtolnay changed the title Parsing error introduced in 1.5 Stricter checking of path expressions in 1.5 Aug 21, 2015
@jakirkham
Copy link
Author

No, that seems correct, it seems to produce the right result, and is a bit more succinct. Is this more efficient, as well?

So, maybe I'm missing something else, but your argument is that select shouldn't check for a particular dictionary/object key. Correct?

@dtolnay
Copy link
Member

dtolnay commented Aug 21, 2015

Here is what the manual has to say about select: https://stedolan.github.io/jq/manual/#%60select%28boolean_expression%29%60

The function select(foo) produces its input unchanged if foo returns true for that input, and produces no output otherwise.

The behavior you were seeing in 1.4 was obviously not this. The = was affecting the object that corresponds to foo in the manual, instead of affecting "the input unchanged." This was due to a bug in the implementation of = and |= in 1.4.

@jakirkham
Copy link
Author

Right, ok. Thanks for the clarification @dtolnay.

@jakirkham
Copy link
Author

So, actually, that ends up needing a little bit more modification as we only want to change outputs or execute_counts if they exist. It turns out they will always exist in an iPython cell that equals "code". So, this works a bit better.

jq '(.cells[] | select(.cell_type == "code") | .outputs) = [] | (.cells[] | select(.cell_type == "code") | .execution_count) = null'

@jakirkham
Copy link
Author

Alternatively, one can do this without the knowledge of the cell value like so.

jq '(.cells[] | select(has("outputs")) | .outputs) = [] | (.cells[] | select(has("execution_count")) | .execution_count) = null'

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

No branches or pull requests

2 participants