Skip to content

Instantly share code, notes, and snippets.

@mcgaffin
Last active August 4, 2016 16:22
Show Gist options
  • Save mcgaffin/97a121271502fc6fcfaf3c3a20e4fa5a to your computer and use it in GitHub Desktop.
Save mcgaffin/97a121271502fc6fcfaf3c3a20e4fa5a to your computer and use it in GitHub Desktop.
Querying json with jq

Install jq

$ brew install jq

Simple Querying

With json that looks like this:

[
  [
    {
      "id": 19,
      "display_name": "Bono Bks",
      "internal_name": "bono_bks",
      "created_at": "2013-08-14T12:38:24.000-04:00",
      "updated_at": "2014-04-25T14:14:36.000-04:00",
      "display_order": 0,
      "seo_name": "bono-bks",
      "visible_in_browse": false,
      "short_name": null,
      "is_disabled": true,
      "region_id": 1,
      "common_name": null,
      "email_name": null,
      "scrapable": null,
      "base_url": null,
      "dynamic": false,
      "common_display_name": "Bono Bks",
      "commission_rate": null,
      "partner_submittable": null,
      "promotable": null,
      "subscribable": null
    },
    ...
  ]
]

You can get the display_name, region_id, and retailer_id of all retailers where region_id == 1 with this:

$ jq ".[] | \
    select(.region_id==1) | \
    {display_name: .display_name, region_id: .region_id, id: .id}" \
    retailers.json

results

{
  "display_name": "Bono Bks",
  "region_id": 1,
  "id": 19
}
{
  "display_name": "Coffee Time",
  "region_id": 1,
  "id": 31
}
{
  "display_name": "DeeperShop",
  "region_id": 1,
  "id": 32
}
{
  "display_name": "Versent",
  "region_id": 1,
  "id": 33
}
{
  "display_name": "Copia",
  "region_id": 1,
  "id": 34
}
{
  "display_name": "Author",
  "region_id": 1,
  "id": 28
}
{
  "display_name": "All Romance",
  "region_id": 1,
  "id": 9
}
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment