John Siu Blog

Tech - Business Tool, Personal Toys

JQ - Json Parser

☰ Table of Content

Cheat sheet for simple usage.

simple.json

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
{
  "id":"01234567",
  "active":true,
  "name":{
    "first":"John",
    "last":"Siu"
  },
  "address": [
    "address line 1",
    "address line 2",
    "address line 3"
  ]
}

Command

CommandUsage
jq '.' simple.jsonPrint pretty
jq '.id' simple.jsonPrint value of id
jq -r '.id' simple.jsonPrint value of id, without quote
jq '.id, .active' simple.jsonPrint value of id and active
jq '.name.last' simple.jsonPrint value of last in name
jq '.address[0]' simple.jsonPrint first item of array address

Wildcard Select

jq don’t have wildcard select as of v1.6. So following does not work:

1
journalctl -o json |jq -r '.| select(.SYSLOG_IDENTIFIER=="postfix*") | .SYSLOG_IDENTIFIER'

Workaround with select(|contains()) or select(|startswith()).

1
2
journalctl -o json |jq -r '.| select(.SYSLOG_IDENTIFIER | contains("postfix")) | .SYSLOG_IDENTIFIER'
journalctl -o json |jq -r '.| select(.SYSLOG_IDENTIFIER | startswith("postfix")) | .SYSLOG_IDENTIFIER'

John Siu

Update: 2022-05-12
comments powered by Disqus