γ Tips on jq
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 -RFor large files,
jqis often the fastest way to view the data
Usejq -Cto output with ANSI color codes andless -Rto properly display them. - Extract From List
-
curl -s https://api.github.com/events \ | jq -r '.[].type' \ | sort | uniq - Use
jq -rto output raw results..refers to the top level of the JSON object.[]addresses each object in an array individually..FIELD_SELECTORselects 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 totrue.|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.| @tsvformats 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,.keyis the field name,.valueis itβs value