参考:https://blog.csdn.net/weixin_44745147/article/details/124035982
ll文件中如下
{
"tcp.alproto.change.detect.alproto.fail": 0
参考:https://alingse.github.io/jq-manual-cn/manual/v1.5/#TypesandValues
处理方式:
cat *.json|jq "path(..)"
输出即全部keys,类似如下:
原文:
Note that the path expressions are not different from normal expressions. The expression path(..|select(type=="boolean")) outputs all the paths to boolean values in ., and only those paths.
examples:
参考:https://hellogitlab.com/OS/Centos/json_tool_jq.html#_4-2-%E5%AF%B9%E8%B1%A1%E6%A0%87%E8%AF%86%E7%AC%A6-%E7%B4%A2%E5%BC%95-foo%E3%80%81-foo-bar
# cat a
{"response_code":0,"verbose_msg":"ok","timestamp":1677555522,"data":{"host":"192.168.120.103","total":534,"schedule":0,"static":251,"engine":0,"dynamic":283,"details":{"dynamic":{"ready ":262,"unacked ":21,"total ":283,"incoming":0.2,"deliver ":0.4,"ack ":0.2},"engine":{"ready ":0,"unacked ":0,"total ":0,"incoming":0,"deliver ":0,"ack ":0},"schedule":{"ready ":0,"unacked ":0,"total ":0,"incoming":0.2,"deliver ":0.2,"ack ":0.2},"static":{"ready ":234,"unacked ":17,"total ":251,"incoming":0,"deliver ":0,"ack ":0}}}}
# cat a|jq -r '.data.details.dynamic|[."total ",."ready ",."unacked ",.incoming,."deliver ",."ack "]| @csv'
283,262,21,0.2,0.4,0.2
参考:https://cloud.tencent.com/developer/ask/sof/37939
打印为CSV/TSV
$ cat file.json | jq -r '.users[] | [.first, .last] | @tsv'
Stevie Wonder
Michael Jacksoncat file.json | jq -r '.users[] | [.first, .last] | @csv'
"Stevie","Wonder"
"Michael","Jackson"第一个表达式.users[]将对象从最外层的数组中取消嵌套,如问题中给出的代码所示。下一个表达式[.first, .last]为每个输入对象创建一个新的值数组,最后一个表达式使用内置函数@tsv和@csv分别将所有输入数组打印为制表符分隔值和逗号分隔值。
https://stackoverflow.com/questions/296536/how-to-urlencode-data-for-curl-command
方法一:
sed -f url_escape.sed
And if you really really feel that you need an external tool (well, it will go a lot faster, and might do binary files and such...) I found this on my OpenWRT router...
replace_value=$(echo $replace_value | sed -f /usr/lib/ddns/url_escape.sed)
Where url_escape.sed was a file that contained these rules:
# sed url escaping
s:%:%25:g
s: :%20:g
s:<:%3C:g
s:>:%3E:g
s:#:%23:g
s:{:%7B:g
s:}:%7D:g
s:|:%7C:g
s:\\:%5C:g
s:\^:%5E:g
s:~:%7E:g
s:\[:%5B:g
s:\]:%5D:g
s:`:%60:g
s:;:%3B:g
s:/:%2F:g
s:?:%3F:g
s^:^%3A^g
s:@:%40:g
s:=:%3D:g
s:&:%26:g
s:\$:%24:g
s:\!:%21:g
s:\*:%2A:g
方法二:
use jq:
$ printf %s 'encode this'|jq -sRr @uri
encode%20this
$ jq -rn --arg x 'encode this' '$x|@uri'
encode%20this
https://github.com/stedolan/jq/issues/1464
.content.book1."field-2"
returns
"value-2"
$ cat tmp.json | jq '.content.book1."field-2"'
"value-2"
$ cat tmp.json | jq '.content.book1["field-three"]'
{
"name": "THIRD",
"url": "book1/field-three/",
"short-url": "book1/field-three/chota-chetan"
}https://stedolan.github.io/jq/manual/
def recurse(f): def r: ., (f | select(. != null) | r); r;
https://stackoverflow.com/questions/42097410/how-to-check-for-presence-of-key-in-jq-before-iterating-over-the-values
$ jq '.result
| .property_history?
| .[]
| select(.event_name == "Sold") #在指定的key中搜索
| .date'
"08/30/2004"JSON:
{
"result":{
"property_history":[
{
"date":"01/27/2016",
"price_changed":0,
"price":899750,
"event_name":"Listed",
"sqft":0
},
{
"date":"12/15/2015",
"price_changed":0,
"price":899750,
"event_name":"Listed",
"sqft":2357
},
{
"date":"08/30/2004",
"price_changed":0,
"price":739000,
"event_name":"Sold",
"sqft":2357
}
]
}
}pip install jq
jq 'keys[]'. For example:echo '{"example1" : 1, "example2" : 2, "example3" : 3}' | jq 'keys[]'
"example1"
"example2"
"example3"
jq is a lightweight command line JSON processor that is very easy to use. Sometimes it is helpful to see your data sorted by a particular field value. Luckily jq makes this easy to do. Here are some sample JSON records we will be working with in this post:
1
2
3
4
5
|
{"first_name":"Savannah","last_name":"Williams"}
{"first_name":"Aria","last_name":"Acevedo"}
{"first_name":"Emma","last_name":"Puckett"}
{"first_name":"Claire","last_name":"Mathis"}
{"first_name":"Ryder","last_name":"Wong"}
|
sort_by() function. The main trick with using the sort_by() function is that your JSON input must be in an array. There are different ways to do this in jq (if your data is not already like this), including using the -s or --slurp option. The --slurp option will read all JSON input into a JSON array. From this point the data can be sorted by value. You can use the .[] syntax to return all elements of the array.
1
2
3
4
5
6
7
8
9
10
11
12
|
#!/bin/bash
$ cat sample.json | jq -s -c 'sort_by(.first_name) | .[]'
{"first_name":"Aaliyah","last_name":"Simpson"}
{"first_name":"Aaliyah","last_name":"Cervantes"}
{"first_name":"Aaliyah","last_name":"Emerson"}
{"first_name":"Aaliyah","last_name":"Fletcher"}
{"first_name":"Aaliyah","last_name":"Hopkins"}
{"first_name":"Aaliyah","last_name":"Schroeder"}
{"first_name":"Aaron","last_name":"Landry"}
...
|
1
2
3
4
5
6
7
8
9
10
11
|
#!/bin/bash
$ cat sample.json | jq -s -c 'sort_by(.first_name, .last_name) | .[]'
{"first_name":"Aaliyah","last_name":"Cervantes"}
{"first_name":"Aaliyah","last_name":"Emerson"}
{"first_name":"Aaliyah","last_name":"Fletcher"}
{"first_name":"Aaliyah","last_name":"Hopkins"}
{"first_name":"Aaliyah","last_name":"Schroeder"}
{"first_name":"Aaliyah","last_name":"Simpson"}
{"first_name":"Aaron","last_name":"Bradley"}
|