Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Error selecting values using paths(node_filter) #2288

Closed
DakshKK opened this issue Mar 19, 2021 · 2 comments · Fixed by #2678
Closed

Error selecting values using paths(node_filter) #2288

DakshKK opened this issue Mar 19, 2021 · 2 comments · Fixed by #2678

Comments

@DakshKK
Copy link

DakshKK commented Mar 19, 2021

Describe the bug
Was trying to delete all null objects/values from my JSON, but using the paths(node_filter) function to generate their paths was unsuccessful.

To Reproduce
Using an example from the Docs, and filter as used on jqplay (using // for comment just over here)

Input : [[],{},1,"foo",null,true,false]
paths(nulls) // Empty o/p -
paths(nulls | not) // o/p - [4]
paths(values) // o/p - [0, 1, 2, 3, 5]
paths(values | not) // o/p - [6]
paths(booleans) // o/p - [5]
paths(booleans | not) // o/p - [6]
paths(. == null) // o/p - [4] | filter is similar to nulls but this works unlike nulls

The same issue seems to extend to all other node_filter it seems, since only true values are produced, for scalar, and false or null values, for scalar | not.

Expected behavior
For any given pre-defined node_filter the output should consist of the values satisfying them.

Input : [[],{},1,"foo",null,true,false]
paths(nulls) // Expected o/p - [4]
paths(nulls | not) // o/p - [0, 1, 2, 3, 5, 6]
paths(values) // o/p - [0, 1, 2, 3, 5, 6]
paths(values | not) // o/p - [4]
paths(booleans) // o/p - [5, 6]
paths(booleans | not) // o/p - [0, 1, 2, 3, 4]
paths(. == null) // o/p - [4] | filter is now similar to nulls

Environment (please complete the following information):

  • OS and Version:
    • Windows 10 (WSL2 Ubuntu 20.04)
    • Linux
      • Ubuntu 20.04
      • Mint 19.3
  • jq version - 1.6
@itchyny
Copy link
Contributor

itchyny commented Mar 19, 2021

Seems to be a dup of #1163. A quick look into the definition of the current paths function, node_filter is expected to output a boolean. So paths(type == "boolean") and paths(isempty(values)|not) will work as expected. When using some select function as its argument, filtering out works due to backtracking, but if the value is falsy, the path is also removed.

 $ echo '[null, false, true]' | jq -c '[leaf_paths]'
[[2]]

@guicho271828
Copy link

This shouldn't be closed, it rather requires an update to the documentation because essentially paths(scalars) is a wrong example.

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

Successfully merging a pull request may close this issue.

3 participants