echo '{ "key1":"value1" }' | jq . { "key1": "value1" }
empty returns no results. None at all. Not even null.
echo '{"key1":"val1"}' | jq '.foo // empty' # empty echo '{"key1":"val1"}' | jq '.key1 // empty' "val1"
jq -r '.[] | select(.InstanceType | test("m2|m3") | not)' test.json
記事:
echo ' { "root":{ "child1":{ "item1":{} }, "child2":{ "item1":{} } } } ' | jq '{"child1":.root.child1}' { "child1": { "item1": {} } }
記事:
echo ' { "ARN": "arn:aws:secretsmanager:ap-northeast-1:123456789012:secret:example-vault-id", "Name": "example-vault-id", "VersionId": "12344567-1234-1234-1234-123445678910", "SecretString": "{\"vault_id\":\"example-vault-id-123\"}", "VersionStages": [ "AWSCURRENT" ], "CreatedDate": "2023-01-02T03:04:05.000000+09:00" }' | jq -r .SecretString | jq -r .vault_id example-vault-id-123
echo '{"num1": 0.123456789}' | jq '.num1 | floor' 0 echo '{"num1": 1.123456789}' | jq '.num1 | floor' 1 echo '{"num1": 1.123456789}' | jq '.num1 * 1000 | floor / 1000' 1.123
echo -e "key1:val1\tkey2:val2\nkey1:val3\tkey2:val4" | jq -s -R 'split("\n")|map(split("\t")|map(split(":")|{"key":.[0],"value":.[1:]|join(":")})|from_entries)' [ { "key1": "val1", "key2": "val2" }, { "key1": "val3", "key2": "val4" }, {} ]
cat test.json | jq -r '.[] | to_entries | map("\(.key):\(.value)") | join("\t")' key1:val1 key2:val2 key1:val3 key2:val4
profile=default region=ap-northeast-1 aws ec2 describe-instances --profile $profile --region $region > ec2raw.json
cat ec2raw.json | jq -r --arg region "$region" '.Reservations[] | .[] | [$region, (.Tags[] | select(.[] == "Name").Value), .InstanceId] | @csv'
cat ec2raw.json | jq -r '.Reservations[].Instances[].BlockDeviceMappings[].Ebs | select(.DeleteOnTermination == false) | .VolumeId'
フィルタを整形し読みやすく出来る。
if has("key1") then .key1 = $arg1 else . end
echo '{"key1":"val1","key2":"val2"}' | jq -f example.jq --arg arg1 "arg1" { "key1": "arg1", "key2": "val2" }
echo '{"key1":"val1","key2":"val2"}' | jq '.key1 = $arg1 | .key2 = $arg2' --arg arg1 "arg1" --arg arg2 "arg2" { "key1": "arg1", "key2": "arg2" }
echo '{"key1":"val1","key2":"val2"}' | jq 'if has("key1") then .key1 = "rep1" else . end' { "key1": "rep1", "key2": "val" }
echo '{"key1":"val1","key2":"val2"}' | jq '.key1 = "rep1" | .key2 = "rep2"' { "key1": "rep1", "key2": "rep2" }
echo '[1, 2, 3]' | jq 'map(.+1)' [ 2, 3, 4 ] # 以下と同じ echo '[1, 2, 3]' | jq '[.[] | .+1]'
echo '{"a": 1, "b": 2, "c": 3}' | jq 'map_values(.+1)' { "a": 2, "b": 3, "c": 4 } # 以下と同じ echo '{"a": 1, "b": 2, "c": 3}' | jq '.[] |= .+1'
echo '[ { "name": "foo", "val": 10 }, { "name": "bar", "val": 20 } ]' | jq -r '.[] | ["\""+.name+"\"", .val] | @tsv' "foo" 10 "bar" 20
echo '{"unixtime": 1575936000}' | jq '(.unixtime | todate)' "2019-12-10T00:00:00Z" echo '{"unixtime": 1575936000}' | jq '(.unixtime | strftime("%Y-%m-%dT%H:%M:%SZ"))' "2019-12-10T00:00:00Z" # 入力元が文字列の場合 echo '{"unixtime": "1575936000"}' | jq '(.unixtime | tonumber | todate)' "2019-12-10T00:00:00Z"
echo '{"date": "2019-12-10"}' | jq '(.date + "T00:00:00Z" | fromdate)' 1575936000
date +%s -d '2019-12-10' 1575903600
echo '[4,3,2]' | jq 'sort' [ 2, 3, 4 ]
echo '[{"id":4}, {"id":3}, {"id":2}]' | jq 'sort_by(.id)' [ { "id": 2 }, { "id": 3 }, { "id": 4 } ]
jq -n '{"key":"foo1", "val": 1},{"key":"foo2","val":null} | select(.val != null)' { "key": "foo1", "val": 1 }
jq -n '["a","b",null] | map(select(.!=null))' [ "a", "b" ]
jq -n '{"key":"foo1", "val": 1},{"key":"foo2","val":null} | del(.val)' { "key": "foo1" } { "key": "foo2" }
echo ' { "ResourceRecordSets": [ { "ResourceRecords": [ { "Value": "ec2-xxx-xxx-xxx-001.ap-northeast-1.compute.amazonaws.com" }, { "Value": "ec2-xxx-xxx-xxx-002.ap-northeast-1.compute.amazonaws.com" } ], "Type": "CNAME", "Name": "roundrobin.example.com.", "TTL": 300 }, { "AliasTarget": { "HostedZoneId": "XXXXXXXXXX", "EvaluateTargetHealth": false, "DNSName": "web-1234567890.ap-northeast-1.elb.amazonaws.com." }, "Type": "A", "Name": "www.example.com." } ] } ' | jq -r '.ResourceRecordSets[] | select(.Type | test("CNAME|AAAA|^A$")) | [.Name, .Type, ([.ResourceRecords[]?.Value, .AliasTarget?.DNSName] | join(","))] |@tsv' roundrobin.example.com. CNAME ec2-xxx-xxx-xxx-001.ap-northeast-1.compute.amazonaws.com,ec2-xxx-xxx-xxx-002.ap-northeast-1.compute.amazonaws.com, www.example.com. A web-1234567890.ap-northeast-1.elb.amazonaws.com.
echo '[{"name":"foo123a"},{"name":"bar123b"}]' | jq '.[] | select(.name | contains("oo"))' { "name": "foo123a" }
echo '[{"name":"foo123a"},{"name":"bar123b"}]' | jq '.[] | select(.name | startswith("foo"))' { "name": "foo123a" }
echo '[{"name":"foo123a"},{"name":"bar123b"}]' | jq '.[] | select(.name | endswith("b"))' { "name": "bar123b" }
echo '{"a":{"enabled":true, "name":"foo"},"b":{"enabled":false, "name":"bar"}}' | jq '. | map(select(.enabled == false))' [ { "enabled": false, "name": "bar" } ]
curl -s localhost:24220/api/plugins.json | jq '.plugins[] | select(.buffer_queue_length > 0)'
select(.Value == "example")
select(.Type | test("CNAME|AAAA|^A$"))
echo '{ "properties": [{ "name": "instanceFamily", "value": "t3" }, { "name": "productDescription", "value": "Linux/UNIX" }, { "name": "instanceType", "value": "t3.medium" } ] }' | jq -r '. | [ (.properties[] | select(.name == "instanceType").value), (.properties[] | select(.name == "instanceFamily").value) ] | @tsv' t3.medium t3
select()する時にnullが混ざっていると「Cannot iterate over null (null)」が出る。
Like .[], but no errors will be output if . is not an array or object.
echo '{"ResourceRecordSets":[{"AliasTarget":{"HostedZoneId":"example","EvaluateTargetHealth":false,"DNSName":"example.ap-northeast-1.elb.amazonaws.com."},"Type":"A","Name":"example.com."},{"ResourceRecords":[{"Value":"example2.ap-northeast-1.elb.amazonaws.com."}],"Type":"CNAME","Name":"www.example.com.","TTL":300}]}' \ | jq '.ResourceRecordSets[] | select(.ResourceRecords[]?.Value | test("example2"))'
echo '[ { "name": "foo", "val": 10 }, { "name": "bar", "val": 20 } ]' | jq -r '.[] | [.name, .val] | @csv' "foo",10 "bar",20
echo '[ { "name": "foo", "val": 10 }, { "name": "bar", "val": 20 } ]' | jq -r '.[] | [.name, .val] | @sh' 'foo' 10 'bar' 20
region=ap-northeast-1 echo '[ { "name": "foo", "val": 10 }, { "name": "bar", "val": 20 } ]' | jq -r --arg region "$region" '.[] | [$region, .name, .val] | @csv' "ap-northeast-1","foo",10 "ap-northeast-1","bar",20
region=ap-northeast-1 echo '[ { "name": "foo", "val": 10 }, { "name": "bar", "val": 20 } ]' | jq -r --arg region "$region," '$region + .[].name' ap-northeast-1,foo ap-northeast-1,bar
echo '{"data": ["0","1","2"]}' | jq -r '.data | join(" ")' 0 1 2 echo '[ { "name": "foo", "val": 10 }, { "name": "bar", "val": 20 } ]' | jq -r '.[] | [.name, .val|tostring] | join(" ")' foo 10 bar 20
echo '[ { "id": 1, "val": 10 }, { "id": 2, "val": 20 } ]' | jq '[.[].val] | length, add, add/length, max, min' 2 # length 30 # add 15 # add/length 20 # max 10 # min
sudo yum install jq --enablerepo=epel
sudo apt install jq