khan.town


fzf, jq, pipes for Fun Command Line Text Processing

Do you ever get a wall of JSON in your commandline and not sure where to go next? Especially if you’re looking for a particular value or a few of them?

Sourced from dummyjson.com/users

Here’s 3 tools to help out of this mess:

  1. jq - jq is a lightweight and flexible command-line JSON processor
  2. fzf - a commandline fuzzy finder
  3. Pipe operator “|”

The core utility here is jq, which helps you process JSON right from your terminal like a pro. You can immediately start by passing it to JQ using the pipe operator as so:

curl https://dummyjson.com/users | jq

Sourced from dummyjson.com/users

Depending on your terminal setup, you should see code highlighting instantly and some prettier formatting!

The fun does not end here. jq has a way to query the JSON object. Since the users are stored in the users array, I can access the users array and pull out just the firstName as so:

curl https://dummyjson.com/users | jq ".users.[].firstName"

Sourced from dummyjson.com/users

There’s plenty of additional querying options that you can read in their documentation. The best part is now you can pass it to the fuzzy finder and search for any text.

curl https://dummyjson.com/users | jq ".users.[].firstName" | fzf

Sourced from dummyjson.com/users

As you can see, the pipe operator helps us pass the output between each utility (curl, jq and fzf). Hopefully you get some mileage out of this!