This Banner is For Sale !!
Get your ad here for a week in 20$ only and get upto 15k traffic Daily!!!

FZF + JQ = Interactive JQ (+ a vim bonus)


fzf + jq



Drawback

Do you continuously question an API and get again a big JSON payload, like:

$ curl https://jsonplaceholder.typicode.com/todos
[
  {
    "userId": 1,
    "id": 1,
    "title": "delectus aut autem",
    "completed": false
  },
  ...
]
Enter fullscreen mode

Exit fullscreen mode

So that you pipe it to jq, however want you could possibly interactively question the information?

Chances are you’ll be aware of some utilities that present this interactivity, like jqp, however these include their very own downsides, specifically refined bugs as a result of re-implementation of the unique JQ in language X, but additionally they’re further dependencies you need to monitor and set up in your platform structure and OS/distribution.



Answer

Here’s a hack (err… ✨ pro-tip ✨) to get JQ interactivity for “free” (so long as you’ve got fzf in your machine):

$ echo '' | fzf --print-query --preview "cat $JSON_FILE_ON_DISK | jq {q}"
$ echo '' | fzf --print-query --preview "cat <(curl $API_URL) | jq {q}"
Enter fullscreen mode

Exit fullscreen mode



How does this work?

echo '' | fzf --print-query --preview "cat $JSON_FILE_ON_DISK | jq {q}"
^^^^^^^   ^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  [1]          [2]                          [3]
Enter fullscreen mode

Exit fullscreen mode

  1. When operating fzf, by default it should immediate you to fuzzy search by way of a listing of recordsdata in your present working listing. So, we suppress this by echoing an empty string into fzf.

  2. --print-query is an non-compulsory flag that can print out the question we typed upon hitting Enter.

  3. --preview flag is what allows all of this. We’re utilizing fzf’s previewing functionality to cat out JSON content material, pipe it to jq, injecting our question as jq arguments through {q} placeholder, and we see the ends in fzf’s preview window.



Additional Enhancements

We will enhance this additional by making a shell script in our world $PATH to show it into a fast helpful utility.

For instance, assuming this script is known as fjq:

#!/bin/bash

echo '' | fzf-tmux -p '80%' --print-query --preview "cat ${1} | jq {q}"
Enter fullscreen mode

Exit fullscreen mode

We will now use this like:

# interactive jq on a single JSON file
$ fjq file.json

# interactive jq on a listing with a number of JSON recordsdata
$ fjq /path/to/*.json

# interactive jq on-the-fly
$ fjq <(curl https://jsonplaceholder.typicode.com/todos)
Enter fullscreen mode

Exit fullscreen mode

Voila! Interactive json querying with simply jq and fzf.



Vim Tip

A semi-interactive expertise might be achieved (with out FZF) by leveraging Vim’s :! and :assist filter to filter contents of Vim buffers by way of an exterior program and write the output again into Vim’s buffers, like:

$ curl https://jsonplaceholder.typicode.com/todos | vim -

# inside vim ...

:%! jq .[].accomplished
:%! kind | uniq -c
Enter fullscreen mode

Exit fullscreen mode

We will even use our earlier script for full JQ interactivity:

:! fjq %
" or
:%! fjq %
" or 
:0learn ! fjq %
Enter fullscreen mode

Exit fullscreen mode

And so forth.



Conclusion

We have seen how terminal packages and shell utilities might be composed to create highly effective workflows.

We took 2-3 instruments that exist already on our system and achieved 80% of what different instruments attempt to accomplish by reimplementing jq in Go, Ruby, Rust, Python, Node.js …and so on.

This isn’t to say you shouldn’t attain for these additional instruments, however, in lots of instances, studying find out how to leverage current instruments allows you to resolve extra issues than what these instruments have been created for.

Blissful hacking! 😄

The Article was Inspired from tech community site.
Contact us if this is inspired from your article and we will give you credit for it for serving the community.

This Banner is For Sale !!
Get your ad here for a week in 20$ only and get upto 10k Tech related traffic daily !!!

Leave a Reply

Your email address will not be published. Required fields are marked *

Want to Contribute to us or want to have 15k+ Audience read your Article ? Or Just want to make a strong Backlink?