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
},
...
]
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}"
How does this work?
echo '' | fzf --print-query --preview "cat $JSON_FILE_ON_DISK | jq {q}"
^^^^^^^ ^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[1] [2] [3]
-
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 byecho
ing an empty string intofzf
. -
--print-query
is an non-compulsory flag that can print out the question we typed upon hitting Enter. -
--preview
flag is what allows all of this. We’re utilizing fzf’s previewing functionality tocat
out JSON content material, pipe it tojq
, injecting our question asjq
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}"
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)
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
We will even use our earlier script for full JQ interactivity:
:! fjq %
" or
:%! fjq %
" or
:0learn ! fjq %
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! 😄