Repo for playing around with jq.
Note
This README.md outputs to files so that you can see the output without having to run the queries yourself.
If no output file is specified jq
will output to the console.
big.json contains the last 1000 commits to the jq repo.
jq '.' < big.json > big_copy.json
.
'.'
formats the json.< big.json
big.json as input.> big_copy.json
writes to big_copy.json.
small.json contains the last 5 commits to the jq repo.
curl 'https://api.github.com/repos/jqlang/jq/commits?per_page=10 | jq '.' > small.json
curl X | jq
pipes the output of curl to jq'.'
formats the json.> small.json
writes to small.json
Use the flag -c
jq '.' -c < big.json
See compacted.json for result.
You can use pipe
within the jq
-filter!
jq '.[] | .commit.message' < big.json
''
jq
-filter is contained in quotes.[]
selects the entire array|
pipes the array to the next part of thejq
-filter.commit.message
format of output; each line is set to the value of[x].commit.message
Alternative syntax: jq '.[].commit.message < big.json
.
See messages for result.
Note
If you'd like to output a valid JSON-array, you will have to construct a different output in the jq
-filter:
jq '[.[] | {message: .commit.message}]' < big.json
- Wrapping the entire filter in
[]
will output a JSON-array - Then you
pipe
the array (.[]
) - And you construct the new structure
{message: .commit.message}
, so that each entry in the array has that format.
See valid_messages.json for result.
You can also query the JSON through the filter.
Say we want every commit where the authors name is shorter than 7 characters
jq '.[] | select(.commit.author.name | length < 7)' < big.json
(short_names.json)
Or every commit where the authors name contains Nico
jq '.[] | select(.commit.author.name | ascii_downcase | contains("nico"))' < big.json
(nico.json)
Or commits where the authors name is shorter than 7 characters AND contains an e
jq '.[] | select((.commit.author.name | length < 7) and (.commit.author.name | ascii_downcase | contains("e")))' < big.json
(short_e.json)
You can still often format and query JSON that's "invalid".
jq -c '.[] | {message: .commit.message}' < big.json | jq '.'
- The first jq command creates a compacted output where each line is
{"message": "example"}
- The second formats it. Output found in prettified_invalid.
jq '.[] | {message: .commit.message, sha: .sha}' < big.json | jq '.sha'
- The first jq command creates a compacted output where each line is
{"message": "example", "sha": "123afb"}
- The second selects all values from the sha-field. Output in shas_from_invalid.
jq '.[] | keys' < big.json
Will output all top-level keys. Output in outputs/all_keys