Search code examples
jqfzf

Run jq in fzf preview with arguments


I have some json data and I want to interactively query it with fzf and jq, by sending the data through stdin and typing the jq query into the fzf query box.

My attempt so far is showing one result in the box, but editing the contents of the query box turns the results blank instead.

fzf-tmux --preview 'jq "$@"  <<< {}'    <<<'[{"x": 1}, {"y": 2}]' 

Solution

  • If you're expecting $@ to be expanded by the shell, then the simple fix is to modify the quoting:

    fzf-tmux --preview 'jq '"$@"' <<< {}'
    

    If on the other hand, you want to use the {q} feature of fzf, which seems to be the case, then you may be out of luck, though whether that's because of a bug in fzf, or some incompatibility between jq and fzf, I cannot tell.

    Navigating by paths

    Let's suppose $JSON is a file containing a single JSON array or object. Then when running the following, you'll see the paths on the LHS, and the value at the selected path on the RHS:

    jq -rc paths "$JSON" |
      fzf-tmux --preview 'x={}; jq "getpath($x)" '"$JSON"