jq使用

日常使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// 提取数组中的元素
// 主要的使用场景是从curl的结果中解析一些数据,这里的result是一个数组

echo '{"result":[{"msg":"hello"},{"msg":"world"}]}' | jq '.result|.[]|.msg'

{
"result": [
{
"msg": "hello"
},
{
"msg": "world"
}
]
}

curl *******
--compressed | jq '.result|.[] |.msg' >> chat_timeout.xkx



// 数据过滤(简单数组)
echo '["apple", "banana", "cherry", "date"]' | jq '.[] | select(. | test("a$") | not)'

echo '["apple", "banana", "cherry", "date"]' | jq '.[] | select(. | test("a$") )'

// 数据过滤(从复杂对象数组中选择)
echo '[{"msg":"hello","code":"34"},{"msg":"world","code":"56"}]' | jq '.[] | select(.["code"] == "34") | .["msg"]' | tr -d '"'


 Comments