His writing on a tablet.
https://www.youtube.com/watch?v=vG0D-kKTF1g
https://www.quantamagazine.org/computer-scientist-donald-knuth-cant-stop-telling-stories-20200416/
jq .
jq type
jq keys
Samples:
cat adb_shared.json | jq keys
cat adb_shared.json | jq .ADB_Instance
cat adb_shared.json | jq .ADB_Instance | head
linux: length (count)
cat adb_shared.json | jq ".ADB_Instance | length"
jq length:
(count)
jq '.data' zcluster.json | jq length
extraer key de cada elemento del array:
iterator operator .[]
jq '.data' zadb.json | jq '.[] | .id'
jq '.data' zadb.json | jq '.[] | ."defined-tags"'
jq '.data' zadb.json | jq '.[] | ."db-name"'
jq '.data' zadb.json | jq '.[] | ."db-name" + " " + (."is-dedicated"|tostring) + ", " + (."defined-tags"|tostring)'
Names with dashes:
You need to enclose in brackets and double quotes:
cat zsl-sample1.json | jq .data | jq '."ingress-security-rules"'
funcion: tostring
has to be inside of quotation mark.
terraform output -json | jq '.elastic_endpoint.value'
alias jqq='jq --color-output < swagger.json | less'
remove additionalProperties for a json
jq 'walk(if type == "object" and has("additionalProperties") then del(.additionalProperties) else . end)' < sw1.json | grep -i add
Samples by quering json:
[
{
"Id": "cb94e7a42732b598ad18a8f27454a886c1aa8bbba6167646d8f064cd86191e2b",
"Names": [
"condescending_jones",
"loving_hoover"
]
},
{
"Id": "186db739b7509eb0114a09e14bcd16bf637019860d23c4fc20e98cbe068b55aa",
"Names": [
"foo_data"
]
},
{
"Id": "a4b7e6f5752d8dcb906a5901f7ab82e403b9dff4eaaeebea767a04bac4aada19",
"Names": [
"jovial_wozniak"
]
},
{
"Id": "76b71c496556912012c20dc3cbd37a54a1f05bffad3d5e92466900a003fbb623",
"Names": [
"bar_data"
]
}
]
contains:
devwebcl@devwebcl-PC ~/tmp
$ cat t.json | jq -c 'map(select(.Names[] | contains ("data"))) '
[{"Id":"186db739b7509eb0114a09e14bcd16bf637019860d23c4fc20e98cbe068b55aa","Names":["foo_data"]},{"Id":"76b71c496556912012c20dc3cbd37a54a1f05bffad3d5e92466900a003fbb623","Names":["bar_data"]}]
devwebcl@devwebcl-PC ~/tmp
$ cat t.json | jq -c 'map(select(.Names[] | contains ("data"))) | .[] .Id'
"186db739b7509eb0114a09e14bcd16bf637019860d23c4fc20e98cbe068b55aa"
"76b71c496556912012c20dc3cbd37a54a1f05bffad3d5e92466900a003fbb623"
Not contains:
devwebcl@devwebcl-PC ~/tmp
$ cat t.json | jq 'map(select(any(.Names[]; contains("data"))|not)|.Id)[]'
"cb94e7a42732b598ad18a8f27454a886c1aa8bbba6167646d8f064cd86191e2b"
"a4b7e6f5752d8dcb906a5901f7ab82e403b9dff4eaaeebea767a04bac4aada19"
devwebcl@devwebcl-PC ~/tmp
$ cat t.json | jq '. - map(select(.Names[] | contains ("data"))) | .[] .Id'
"cb94e7a42732b598ad18a8f27454a886c1aa8bbba6167646d8f064cd86191e2b"
"a4b7e6f5752d8dcb906a5901f7ab82e403b9dff4eaaeebea767a04bac4aada19"
https://github.com/stedolan/jq/wiki/Cookbook#filter-objects-based-on-the-contents-of-a-key
jq -s (slurp), concatenates json
--slurp (-s) key is needed and map() to do so in one shot
$ cat f1.json
{
"records": [
{"a": 1},
{"a": 3}
]
}
$ cat f2.json
{
"records": [
{"a": 2}
]
}
$ jq -s 'map(.records[].a)' f?.json
[
1,
3,
2
]
# concatenamos arrays:
jq -s '.[0] + .[1]' sample1.json sample2.json