γ€ˆ Tips on jq

Jan 27, 2023 β€’ ✎ edit

jq is a fast, streaming JSON parser accessible from the command line, capable of reading and transforming well-formatted JSON. Below are some references of actions you can do with jq:

Streaming Large Data
node <<< "for(let i = 0; i < 1_000_000; i++) console.log(JSON.stringify({ i, r: Math.random()}))" \
| jq -C | less -R

For large files, jq is often the fastest way to view the data
Use jq -C to output with ANSI color codes and less -R to properly display them.

Extract From List
curl -s https://api.github.com/events \
| jq -r '.[].type' \
| sort | uniq
Use jq -r to output raw results.
  • . refers to the top level of the JSON object.
  • [] addresses each object in an array individually.
  • .FIELD_SELECTOR selects a specific field.
Select From Array
curl -s https://api.github.com/events \
| jq '.[]
  | select(.type == "PushEvent")
  | .payload.commits[].message'
  • select(BOOLEAN_EXPRESSION) applies a boolean expression to each element and retains only those that evaluate to true.
  • | pipes results to the next step, similar to Unix pipes.
Map and format
curl -s https://api.github.com/events \
| jq -r '.[]
    | select(.type == "PushEvent")
    | .payload.commits[]
    | [.author.email, .sha, .message]
    | @tsv'
  • | [.FIELD_SELECTOR1, .FIELD_SELECTOR2] creates an array with the results of each selector.
  • | @tsv formats the output as a tab-separated table (other formats include @csv, @json, @uri, @base64).
String Interpolation
curl -s https://api.github.com/events \
| jq -r '.[]
  | "\(.actor.display_login) - \(.type) on repo:\(.repo.name) at date:\(.created_at)"
'
Map Objects
curl -s https://api.github.com/events \
| jq '.[] | to_entries | map([.key, .value])'
  • to_entries - iterates through all object fields
  • | map - create a new object using, .key is the field name, .value is it’s value
Leave a Comment!