JQ

JQ, the hidden treasure in the family of CLI tools. This one has personaly saved me so much time its hard to qualify just how important it is


















Lets create a file with the following content

{"type": "foo", "values": [1, 2, 3, 4, 5]}
{"type": "foo", "values": [69, 420, 42, 69420]}
{"type": "bar", "values": {"a": 42, "b": 69}}
{"type": "bar", "values": {"a": 1337, "b": 420}}
{"type": "bar", "values": {"a": 111, "b": 222}}

















prettify logs

cat out | jq  # jq '' out

















compact json

cat out | jq | jq -c

















check this out

{
  "type": "foo",
  "values": [
    1,
    2,
    3,
    4,
    5
  ]
}
{
  "type": "foo",
  "values": [
    69,
    420,
    42,
    69420
  ]
}
{
  "type": "bar",
  "values": {
    "a": 42,
    "b": 69
  }
}
{
  "type": "bar",
  "values": {
    "a": 1337,
    "b": 420
  }
}
{
  "type": "bar",
  "values": {
    "a": 111,
    "b": 222
  }
}

















lets look at the data

Lets sum ALL the foo's values arrays PER struct

{"type": "foo", "values": [1, 2, 3, 4, 5]}
{"type": "foo", "values": [69, 420, 42, 69420]}
{"type": "bar", "values": {"a": 42, "b": 69}}
{"type": "bar", "values": {"a": 1337, "b": 420}}
{"type": "bar", "values": {"a": 111, "b": 222}}
➜  dev-prod-2 git:(main) ✗ cat out | jq 'select(.type == "foo") | .values | add'

















lets do it again

Lets sum ALL the foo's values arrays and add it as a key to type: foo valueSum

{"type": "foo", "values": [1, 2, 3, 4, 5]}
{"type": "foo", "values": [69, 420, 42, 69420]}
{"type": "bar", "values": {"a": 42, "b": 69}}
{"type": "bar", "values": {"a": 1337, "b": 420}}
{"type": "bar", "values": {"a": 111, "b": 222}}
➜  dev-prod-2 git:(main) ✗ cat out | jq 'select(.type == "foo") | .valueSet = (.values | add)'


We can filter out the valueSet now

➜  dev-prod-2 git:(main) ✗ cat out | jq 'select(.type == "foo") | .valueSet = (.values | add) | select(.valueSet > 20)'