Skip to content

Instantly share code, notes, and snippets.

@chenchun
Last active June 9, 2022 12:10
Show Gist options
  • Save chenchun/a9e43a09d809b4429397838d3f1f55e9 to your computer and use it in GitHub Desktop.
Save chenchun/a9e43a09d809b4429397838d3f1f55e9 to your computer and use it in GitHub Desktop.
jq

https://stackoverflow.com/questions/34477547/how-to-combine-the-sequence-of-objects-in-jq-into-one-object

{
  "a": "green",
  "b": "white"
}
{
  "a": "red",
  "c": "purple"
}

into one object:

{
  "a": "red",
  "b": "white",
  "c": "purple"
}

Also, how can I wrap the same sequence into an array?

[
    {
      "a": "green",
      "b": "white"
    },
    {
      "a": "red",
      "c": "purple"
    }
]

Thus one way to combine objects in the input stream is to use:

jq -s add

For the second problem, creating an array:

jq -s .

transform

cat allips.json
{
 "last": true,
 "totalElements": 3898,
 "totalPages": 1,
 "first": true,
 "numberOfElements": 3898,
 "size": 9999,
 "number": 0,
 "content": [
  {
   "ip": "9.135.64.10",
   "policy": 0,
   "updateTime": "2020-12-30T13:35:42.327160012Z",
   "releasable": true
  },
  {
   "ip": "9.135.64.100",
   "policy": 0,
   "updateTime": "2020-12-30T13:43:43.059217922Z",
   "releasable": true
  },
  {
   "ip": "9.135.64.101",
   "policy": 0,
   "updateTime": "2020-12-30T13:47:20.33446172Z",
   "releasable": true
  }]
}

cat allips.json | jq '.content[]| select( .releasable == true and .status == "Deleted" )|{ip: .ip, appType: .appType, appName: .appName, podName: .podName, namespace: .namespace}' | jq -s '{ips:.}'

https://stedolan.github.io/jq/tutorial/

array

jq '.data[].content'

# multiple fields
jq '.data[]|.content,.sendTime'


# multiple fields to join as a line
cat DescribeNetworkInterfaces | jq -r '.NetworkInterfaceSet[]|[.NetworkInterfaceId,.Attachment.InstanceId]|join(" ")'

# output value without double quotas
jq -r

# quota in key
jq '.beans[]| select( .PendingContainers > 10000 )|[.PendingContainers,.modelerType,."tag.Queue"]|join(" ")'

# filter by value
curl -s http://1.x.x.x:8080/jmx?qry=Hadoop:* | jq '.beans[]| select(  ."tag.Queue" == "root.wx.xx")'

audit日志

jq 'select(.requestURI|startswith("/api/v1/pods"))|[.requestReceivedTimestamp,.verb,.requestURI,.user.username]|join(" ")' /root/rami/kubernetes-2022-03-21T12-* | grep -v 'allowWatchBookmarks\|watch'
jq '.items[]| "\(.metadata.name) \(.spec.dedicatedNodes|to_entries|.[0].key) "' physicalcluster.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment