Skip to content

Instantly share code, notes, and snippets.

@fabienhinault
Last active February 17, 2025 13:45
Show Gist options
  • Save fabienhinault/fc7f655a696f302bcb4bcdf15250ae4c to your computer and use it in GitHub Desktop.
Save fabienhinault/fc7f655a696f302bcb4bcdf15250ae4c to your computer and use it in GitHub Desktop.
complementary help on jq
body: |
Manual id at
https://github.com/stedolan/jq/blob/master/docs/content/3.manual/manual.yml
sections:
- title: "\"id\": null is equivalent to no \"id\""
examples:
- program: '.[] | select(.id)'
input: '[{"id": null, "val": 1}, {"id": "second", "val": 2}]'
output: ['{"id": "second", "val": 2}']
- title: "To get all ids in a sparse json file, where the path may not exist"
examples:
- program: ' map( select(.a) | select(.a[].b) | select(.a[].b[].c) | .id ) | unique '
input:
output:
- title: "To add an object with some random value to the array "valeurs" of each object in an array"
examples:
- program: '[ .[] | [{"nom": "nom", "valeur": (now | . as $n | floor | $n - . | . * 2147483648 | randomize(.) | .Bits | . % 400 | . / 5 | floor | . * 5)}] as $r | .valeurs += $r ]'
intput: '[{"valeurs":[{"nom1": "nom1", "valeur1": 1}, {"nom2": "nom2", "valeur2": 2}]}]'
output: '[{"valeurs":[{"nom1": "nom1", "valeur1": 1}, {"nom2": "nom2", "valeur2": 2}]}, {"nom": "nom", "valeur": <multiple de 5 entre 0 et 400>}]}]'
- title: "To change the name of some properties deep in a json object"
examples:
- program: '(.[] | .stuff | .[]) |= . + {"aa": .a, "bb": .b} | del(.[] | .stuff | .[] | .a) | del(.[] | .stuff | .[] | .b)'
intput:
output:
- title: "To transform the array representing a graph to the array of neighbours"
examples:
- program: '. | reduce .[] as [$i, $j] ([]; .[$i] += [$j] | .[$j] += [$i])'
intput: [[0, 6], [0, 8], [1, 7], [1, 8], [2, 7], [2, 8], [3, 7], [3, 8], [4, 7], [4, 8], [5, 7], [5, 8], [6, 8]]
output: [[6, 8], [7, 8], [7, 8], [7, 8], [ 7, 8 ], [ 7, 8 ], [ 0, 8 ], [ 1, 2, 3, 4, 5 ], [ 0, 1, 2, 3, 4, 5, 6 ]]
- title: "To select running pods and display name and env"
examples:
- program: '.items[] | select(.status.containerStatuses[].state | has("running")) | [.metadata.name , (.spec.containers[0].env[] | select(.name == "UV_CONFIG_DATA")) , (.status.containerStatuses[].state | keys | .[]) ]'
intput: sudo kubectl get pods -ojson
output: [ {
"name": "UV_CONFIG_DATA",
"value": "{\"publisher\":{\"type\":\"Standard Output\",\"configuration\":{\"format\":\"pretty_json\"}},\"test\":{\"type\":\"Continuous TWAMP\",\"configuration\":{\"target\":[{\"host\":\"172.30.55.5\",\"port\":6000}],\"payloadSize\":160,\"lastPacketTimeout\":2000,\"lossThreshold\":50,\"unavailableThreshold\":10,\"availableThreshold\":10,\"reportUnavailPeriods\":false,\"percentageLo\":25,\"percentageMed\":50,\"percentageHi\":75,\"percentJitterLo\":25,\"percentJitterMed\":50,\"percentJitterHi\":75,\"diffservCodepoint\":0,\"enableOneWayLoss\":\"YES\",\"portNumLow\":50000,\"portNumHigh\":50050,\"dynamicFallback\":\"FALLBACK\",\"twampMode\":\"LIGHT\",\"reportInterval\":60,\"OneWayDisplay\":\"SyncClocks\",\"timestampFormat\":\"NTP\",\"zeroServerStartTime\":false,\"insertSenderTimestamps\":false,\"enableSyntheticMOS\":false,\"syntheticCodec\":\"G.711 at 20ms\",\"HAvirtSync\":\"NO\",\"packetInterval\":4500}},\"authorizer\":{\"type\":\"File\",\"configuration\":{\"path\":\"/tmp/authorizer/authorizer.json\"}}}"
},
"running"
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment