path/1
as a simpler eval
just for path expressions
#3198
Replies: 1 comment
-
Hi, that would be nice. And yes i think a If you want $ jq --arg p '.a[range(5) | select(.%2==0)]' -L . 'include "jqjq"; eval($p)' <<< '{"a": [0,1,2,3,4]}'
0
2
4
$ fq --arg p '.a[range(5) | select(.%2==0)]' 'eval($p)' <<< '{"a": [0,1,2,3,4]}'
0
2
4
# also has expr_to_path/path_to_expr
$ fq -n '".a" | expr_to_path | path_to_expr'
".a" BTW thanks for gist link! nice |
Beta Was this translation helpful? Give feedback.
-
When calling
jq
from a shell script, I find that parameterizing paths that need to get evaluated is not very ergonomic:PATHS
array to jq with--argjson
, but writing paths as a JSON array gets quickly boring--arg
and somehow parsing it into aPATHS
array, e.g.:$path | split(".")[1:]
, but this only accepts a limited subset of all path expressions supported by jqIn the long term, an eval function (#384) would add built-in support for evaluating paths passed as strings, but without going that far, would it be possible to make a variant of path/1 that accepts the path expression as a string?
I suppose that limiting the dynamic evaluation to just paths instead of whole jq programs makes it possible to sidestep the issues mentioned for
eval
(e.g. no need to handle contexts and whatnot), in particular since the functions simply turns the string into aPATHS
array without having to insert it into the evaluation pipeline.Or is there a limitation in the current implementation that makes it impossible to evaluate paths dynamically without the machinery that
eval
would need?Beta Was this translation helpful? Give feedback.
All reactions