jq-test|⇒ cat test.json| jq '.["foo"]'
42
jq-test|⇒ cat test.json
{"foo": 42, "bar": "less interesting data"}
jq-test|⇒ cat test.json| jq .foo
42
jq-test|⇒ cat test.json| jq .no
null
# 有特殊字符时的查看方法
jq-test|⇒ cat test.json| jq '.["foo"]'
42
jq-test|⇒ cat test.json
{"foo": 42, "bar": "less interesting data","42":12}
jq-test|⇒ cat test.json| jq '.["42"]'
12
jq-test|⇒ cat test.json| jq '."42"'
12
jq-test|⇒ cat test.json| jq '.foo?'
42
jq-test|⇒ cat test.json| jq '.foo7?'
null
jq-test|⇒ cat test.json| jq '[.foo?]'
[
42
]
If the key contains special characters or starts with a digit, you need to surround it with double quotes like this: ."foo$"
, or else .["foo$"]
.
数组
jq-test|⇒ cat 2.json
[{"name":"JSON", "good":true}, {"name":"XML", "good":false}]
jq-test|⇒ cat 2.json | jq .
[
{
"name": "JSON",
"good": true
},
{
"name": "XML",
"good": false
}
]
jq-test|⇒ cat 2.json | jq '.[]'
{
"name": "JSON",
"good": true
}
{
"name": "XML",
"good": false
}
jq-test|⇒ cat 2.json | jq '.[0]'
{
"name": "JSON",
"good": true
}
jq-test|⇒ cat 2.json | jq '.[2]'
null
# 倒数第一个
jq-test|⇒ cat 2.json | jq '.[-1]'
{
"name": "XML",
"good": false
}
jq-test|⇒ cat 2.json | jq '.[-2]'
{
"name": "JSON",
"good": true
}
切片:
jq-test|⇒ echo '["a","b","c","d","e"]' | jq '.[2:4]'
[
"c",
"d"
]
jq-test|⇒ echo '["a","b","c","d","e"]' | jq '.[:4]'
[
"a",
"b",
"c",
"d"
]
jq-test|⇒ echo '["a","b","c","d","e"]' | jq '.[-2:]'
[
"d",
"e"
]
jq-test|⇒ echo '"abcdefghi"' | jq '.[2:4]'
"cd"
# 返回所有的value
jq-test|⇒ cat test.json
{"foo": 42, "bar": "less interesting data","42":12}
jq-test|⇒ cat test.json | jq '.[]'
42
"less interesting data"
12