Personal Knowledge Base

A personal knowledge base

Jq

jq is like awk for JSON.

#Examples

#Find Current Time Entry

Where the current time entry is defined to be the one that is currently running. I.e. not ended.

curl -s "http://morty.trikeapps.com/time_entries?api_token=xxx" | jq --raw-output '.time_entries[] | select(.end == null) | .task_name'

#Print all Values of a JSON Object​

jq --raw-output 'values[]' file.json

#Swap the Keys and Values of an Object

​ Input is of the form:

{
  "foo": "bar",
  "haz": "baz"
}

The following two expressions are equivalent:

jq 'to_entries | map({key: .value, value: .key}) | from_entries' sample.json
jq 'with_entries({key: .value, value: .key})' sample.json

​

#Extract Dates from Array of JSON Objects

Given input like:

[
  {
    "elapsed": 9,
    "error": 0,
    "charCount": 43,
    "backspaceCount": 0,
    "incorrectCount": 0,
    "typedCharCount": 43,
    "collateralCount": 0,
    "overhead": 0,
    "minutesDecimal": 0.2,
    "unproductiveKeystrokes": 0,
    "wpm": 57,
    "elapsedPretty": "0:09",
    "elapsedPrettyLong": "0m 09s",
    "lesson": "/user/lesson/Ruby/Linked%20List/recently_changed_list.rb/2",
    "finishTime": 1451595856
  },
  {
    "elapsed": 281,
    "error": 4.1,
    "charCount": 716,
    "backspaceCount": 42,
    "incorrectCount": 29,
    "typedCharCount": 800,
    "collateralCount": 12,
    "overhead": 12,
    "minutesDecimal": 4.7,
    "unproductiveKeystrokes": 84,
    "wpm": 31,
    "elapsedPretty": "4:41",
    "elapsedPrettyLong": "4m 41s",
    "lesson": "/user/lesson/Ruby/Radiopaedia/image.rb/3",
    "finishTime": 1451681474
  },
]

The following command outputs the dates:

jq --raw-output 'map(.finishTime | gmtime | todate)[]' /Users/wmoore/Downloads/typing-analytics.json | sed 's/T.*$//' | sort -u

like:

2015-12-29
2015-12-31
Last modified: 28 March 2016