Skip to content

Instantly share code, notes, and snippets.

@dhval
Last active November 12, 2022 07:34
Show Gist options
  • Save dhval/26e6f9de99bd60fc223c to your computer and use it in GitHub Desktop.
Save dhval/26e6f9de99bd60fc223c to your computer and use it in GitHub Desktop.
JQ - Command Line JSON Parser

Manual -r Ignore result format -f read filter from file |= Passes the values

Filter .items array, select & print id from file .json

jq -r --arg id "$id"  '.items[] | select(.id == $id) | .id' file.json

Filter and transforrm

jq -r '{i: .id, t: .title}'
  • .[] returns each element of the array returned in the response.
  • [.org,.url] converts output to array
  • @tsv (newer version) tab separator
.[] | {org: .agency, topic: .topic, url: .endpoint.url} | select(.topic == "CourtCaseEvent") | [.org,.url] | @tsv
  • sort_by
sort_by(.topic) | .[] | {org: .agency, topic: .topic, url: .endpoint.url} | [.topic,.org,.url] | @csv

Without @csv using string interpolation ()

sort_by(.topic) | .[] | {org: .agency, topic: .topic, url: .endpoint.url} | "\(.topic)\t\(.org)\t\(.url)"

f

jq '.[] | {action: .category, id: .fileId, trackingId: .trackingId, time: .sentDateTime} | [.trackingId, .action, .category, .id, .time]  | @csv' file.json

Resource

jq-json faq

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment