Skip to content

Instantly share code, notes, and snippets.

@keidarcy
Last active January 16, 2022 08:11
Show Gist options
  • Save keidarcy/cfe40132f17a1d4255350e9b7dfd725e to your computer and use it in GitHub Desktop.
Save keidarcy/cfe40132f17a1d4255350e9b7dfd725e to your computer and use it in GitHub Desktop.
jq examples

Simple jq command

select, -r, length, reverse

curl -s "https://jsonplaceholder.typicode.com/todos" | jq '.[].completed'
curl -s "https://jsonplaceholder.typicode.com/todos" | jq '.[] | select(.completed==true)'
curl -s "https://jsonplaceholder.typicode.com/todos" | jq '.[] | select(.completed==true) | { title: .title }'
curl -s "https://jsonplaceholder.typicode.com/todos" | jq '.[] | select(.completed==true) | .title,.userId'
curl -s "https://jsonplaceholder.typicode.com/todos" | jq -r '.[] | select(.completed==true) | .title,.userId'

sort_by, group_by, limit

curl -s "https://openlibrary.org/search.json?q=spiderman"
curl -s "https://openlibrary.org/search.json?q=spiderman&limit=5"
curl -s "https://openlibrary.org/search.json?q=spiderman&limit=5" | less
curl -s "https://openlibrary.org/search.json?q=spiderman" > openlibrary.json
jq '.docs[] | {title}' openlibrary.json
jq '.docs[] | {title, publish_year}' openlibrary.json
jq '.docs[] | {title, publish_year: .publish_year[0]}' openlibrary.json
jq '.docs[] | {title, author_name: .author_name, publish_year: .publish_year[0]}' openlibrary.json
jq '.docs[] | {title, author_name: .author_name[0], publish_year: .publish_year[0]}' openlibrary.json
jq '.docs[] | {title, author_name: .author_name[0], publish_year: .publish_year[0]} | select(.publish_year != null and .author_name != null)' openlibrary.json
jq '.docs[] | {title, author_name: .author_name[0], publish_year: .publish_year[0]} | select(.publish_year != null and .author_name != null) | count' openlibrary.json
jq '[.docs[] | {title, author_name: .author_name[0], publish_year: .publish_year[0]} | select(.publish_year != null and .author_name != null)] ' openlibrary.json
jq '[.docs[] | {title, author_name: .author_name[0], publish_year: .publish_year[0]} | select(.publish_year != null and .author_name != null)] | sort_by(.publish_year)' openlibrary.json
jq '[.docs[] | {title, author_name: .author_name[0], publish_year: .publish_year[0]} | select(.publish_year != null and .author_name != null)] | sort_by(.publish_year) | reverse' openlibrary.json
jq '[.docs[] | {title, author_name: .author_name[0], publish_year: .publish_year[0]} | select(.publish_year != null and .author_name != null)] | sort_by(.publish_year) | reverse | length' openlibrary.json
jq '[.docs[] | {title, author_name: .author_name[0], publish_year: .publish_year[0]} | select(.publish_year != null and .author_name != null)] | sort_by(.publish_year) | reverse | [limit(3;.[])]' openlibrary.json
jq '[.docs[] | {title, author_name: .author_name[0], publish_year: .publish_year[0]} | select(.publish_year != null and .author_name != null)] | group_by(.author_name)' openlibrary.json
jq '[.docs[] | {title, author_name: .author_name[0], publish_year: .publish_year[0]} | select(.publish_year != null and .author_name != null)] | group_by(.author_name) | .[]' openlibrary.json
jq '.docs[] | {title, author_name: .author_name[0], publish_year: .publish_year[0]} | select(.publish_year != null and .author_name != null) | group_by(.author_name)' openlibrary.json
jq '[.docs[] | {title, author_name: .author_name[0], publish_year: .publish_year[0]} | select(.publish_year != null and .author_name != null)] | group_by(.author_name) | .[] | { author_name: .[0].author_name }' openlibrary.json
jq '[.docs[] | {title, author_name: .author_name[0], publish_year: .publish_year[0]} | select(.publish_year != null and .author_name != null)] | group_by(.author_name) | .[] | { author_name: .[0].author_name, count: . }' openlibrary.json
jq '[.docs[] | {title, author_name: .author_name[0], publish_year: .publish_year[0]} | select(.publish_year != null and .author_name != null)] | group_by(.author_name) | .[] | { author_name: .[0].author_name, count: . | length }' openlibrary.json
jq '[.docs[] | {title, author_name: .author_name[0], publish_year: .publish_year[0]} | select(.publish_year != null and .author_name != null)] | group_by(.author_name) | [.[] | { author_name: .[0].author_name, count: . | length }]' openlibrary.json
jq '[.docs[] | {title, author_name: .author_name[0], publish_year: .publish_year[0]} | select(.publish_year != null and .author_name != null)] | group_by(.author_name) | [.[] | { author_name: .[0].author_name, count: . | length }] | sort_by(.count)' openlibrary.json
jq '[.docs[] | {title, author_name: .author_name[0], publish_year: .publish_year[0]} | select(.publish_year != null and .author_name != null)] | group_by(.author_name) | [.[] | { author_name: .[0].author_name, count: . | length }] | sort_by(.count) | reverse | [limit(3:.[])]' openlibrary.json
jq '[.docs[] | {title, author_name: .author_name[0], publish_year: .publish_year[0]} | select(.publish_year != null and .author_name != null)] | group_by(.author_name) | [.[] | { author_name: .[0].author_name, count: . | length }] | sort_by(.count) | reverse' openlibrary.json
jq '[.docs[] | {title, author_name: .author_name[0], publish_year: .publish_year[0]} | select(.publish_year != null and .author_name != null)] | group_by(.author_name) | [.[] | { author_name: .[0].author_name, count: . | length }] | sort_by(.count) | reverse | [limit(3;.[])]' openlibrary.json

Convert a JSON file to the CSV one

curl -s "https://jobs.github.com/positions.json?description=kotlin" > jobs.json

jq -r 'map({id,title,url,company,location}) | (first | keys_unsorted) as $keys | map([to_entries[] | .value]) as $rows | $keys,$rows[] | @csv' jobs.json > jobs.csv

cat jobs.csv
"id","title","url","company","location"
"2ededc50-3c18-4d2c-ba54-95151ca4209a","Android Engineer","https://jobs.github.com/positions/2ededc50-3c18-4d2c-ba54-95151ca4209a","Trade Republic Bank GmbH","Berlin"
"2cc06700-d9ac-45ad-9bd7-a7e5d3c29b6f","Software Engineer - Android","https://jobs.github.com/positions/2cc06700-d9ac-45ad-9bd7-a7e5d3c29b6f","AiCure","Remote"
"242132e0-f129-4a1b-8bfa-30f2fed13123","Senior Backend Developer (Data Science)","https://jobs.github.com/positions/242132e0-f129-4a1b-8bfa-30f2fed13123","komoot","Remote Europe"
"fd8c3b7c-ccfe-4cc1-882c-ef9c0522903e","Software Engineer","https://jobs.github.com/positions/fd8c3b7c-ccfe-4cc1-882c-ef9c0522903e","HBM nCode Federal LLC","Starkville, MS"
"7d84945b-893d-493e-957a-4ed9e6f84607","Software Engineer","https://jobs.github.com/positions/7d84945b-893d-493e-957a-4ed9e6f84607","HBM nCode Federal LLC","Southfield, MI"
"2231d64f-e79b-4036-8601-24b2717b2896","Senior Fullstack / Flutter Developer (m/f/d)","https://jobs.github.com/positions/2231d64f-e79b-4036-8601-24b2717b2896","Superlist","Remote"
"caa90907-8252-4732-a815-06d96f1348bb","Senior Software Engineer - Mobile (m/f/d)","https://jobs.github.com/positions/caa90907-8252-4732-a815-06d96f1348bb","BASF Digital Farming GmbH","Köln"

Merging JSON files

{
  "id": "324a9e6f-44e8-4d70-9645-7247e5385d05",
  "name": "Product old name",
  "prices": [
    {
      "amount": 19.99,
      "currency": "USD"
    }
  ],
  "tags": [
    "tech",
    "book",
    "programming"
  ],
  "prototype": false,
  "meta": {
    "test": 1
  },
  "nested": {
    "map": {
      "1": "2",
      "4": 0
    },
    "list": [
      1,
      2,
      3
    ]
  }
}
{
  "id": "324a9e6f-44e8-4d70-9645-7247e5385d05",
  "name": "Product new name",
  "vendor": "Vendor Name Inc.",
  "prices": [
    {
      "amount": 19.99,
      "currency": "CAD"
    }
  ],
  "tags": [
    "programming",
    "learning"
  ],
  "meta": {
    "beta": 0
  },
  "nested": {
    "map": {
      "1": 3,
      "2": 1
    },
    "list": [
      2,
      3,
      4
    ]
  }
}
jq -s 'def deepmerge(a;b):
  reduce b[] as $item (a;
    reduce ($item | keys_unsorted[]) as $key (.;
      $item[$key] as $val | ($val | type) as $type | .[$key] = if ($type == "object") then
        deepmerge({}; [if .[$key] == null then {} else .[$key] end, $val])
      elif ($type == "array") then
        (.[$key] + $val | unique)
      else
        $val
      end)
    );
  deepmerge({}; .)' product1.json product2.json > merged.json
{
  "id": "324a9e6f-44e8-4d70-9645-7247e5385d05",
  "name": "Product new name",
  "prices": [
    {
      "amount": 19.99,
      "currency": "CAD"
    },
    {
      "amount": 19.99,
      "currency": "USD"
    }
  ],
  "tags": [
    "book",
    "learning",
    "programming",
    "tech"
  ],
  "prototype": false,
  "meta": {
    "test": 1,
    "beta": 0
  },
  "nested": {
    "map": {
      "1": 3,
      "4": 0,
      "2": 1
    },
    "list": [
      1,
      2,
      3,
      4
    ]
  },
  "vendor": "Vendor Name Inc."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment