我有这个Json
{
"users": [
{
"first": "Stevie",
"last": "Wonder"
},
{
"first": "Michael",
"last": "Jackson"
}
]
}
Run Code Online (Sandbox Code Playgroud)
使用jq我想连续显示名字和姓氏.像这样 -
Stevie Wonder
Michael Jackson
Run Code Online (Sandbox Code Playgroud)
这是我走了多远 -
jq '.users[].first, .users[].last'
Run Code Online (Sandbox Code Playgroud)
但它显示出来
"Stevie"
"Michael"
"Wonder"
"Jackson"
Run Code Online (Sandbox Code Playgroud)
请注意以下事项 -
Eri*_*ord 284
我建议使用String Interpolation:
jq '.users[] | "\(.first) \(.last)"'
Run Code Online (Sandbox Code Playgroud)
abr*_*ham 161
您可以使用addition来连接字符串.
串通过被结合成一个较大的字符串添加.
jq '.users[] | .first + " " + .last'
Run Code Online (Sandbox Code Playgroud)
ing*_*net 75
除了其他人的建议之外,我认为还有两个选择值得一提。
$ cat file.json | jq -r '.users[] | [.first, .last] | @tsv'
Stevie Wonder
Michael Jackson
Run Code Online (Sandbox Code Playgroud)
cat file.json | jq -r '.users[] | [.first, .last] | @csv'
"Stevie","Wonder"
"Michael","Jackson"
Run Code Online (Sandbox Code Playgroud)
第一个表达式 ,.users[]从最外层数组中取消对象的嵌套,如问题中给出的代码所示。下一个表达式[.first, .last]为每个输入对象创建一个新的值数组,最终表达式使用内置函数@tsv和@csv将所有输入数组分别打印为制表符分隔和逗号分隔的值。
同样,可以再次构造 JSON 值,如果您只想保留字段的子集,这会很有趣:
$ cat file.json | jq -c '.users[] | {first}'
{"first":"Stevie"}
{"first":"Michael"}
Run Code Online (Sandbox Code Playgroud)
小智 14
我的方法将是(您的 json 示例格式不正确.. 猜测那只是一个示例)
jq '.Front[] | [.Name,.Out,.In,.Groups] | join("|")' front.json > output.txt
Run Code Online (Sandbox Code Playgroud)
返回这样的东西
"new.domain.com-80|8.8.8.8|192.168.2.2:80|192.168.3.29:80 192.168.3.30:80"
"new.domain.com -443|8.8.8.8|192.168.2.2:443|192.168.3.29:443 192.168.3.30:443"
Run Code Online (Sandbox Code Playgroud)
并使用正则表达式 grep 输出。
mac*_*qcq 13
虽然上面的两个答案都很好,如果key,value是字符串,我有一种情况可以追加一个字符串和整数(jq错误使用上面的表达式)
要求:在json下面构建一个url
pradeep@seleniumframework>curl http://192.168.99.103:8500/v1/catalog/service/apache-443 | jq .[0]
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 251 100 251 0 0 155k 0 --:--:-- --:--:-- --:--:-- 245k
{
"Node": "myconsul",
"Address": "192.168.99.103",
"ServiceID": "4ce41e90ede4:compassionate_wozniak:443",
"ServiceName": "apache-443",
"ServiceTags": [],
"ServiceAddress": "",
"ServicePort": 1443,
"ServiceEnableTagOverride": false,
"CreateIndex": 45,
"ModifyIndex": 45
}
Run Code Online (Sandbox Code Playgroud)
解:
curl http://192.168.99.103:8500/v1/catalog/service/apache-443 |
jq '.[0] | "http://" + .Address + ":" + "\(.ServicePort)"'
Run Code Online (Sandbox Code Playgroud)
小智 6
这将产生一个名称数组
> jq '[ .users[] | (.first + " " + .last) ]' ~/test.json
[
"Stevie Wonder",
"Michael Jackson"
]
Run Code Online (Sandbox Code Playgroud)
通过做这样的事情,我非常接近我想要的
cat my.json | jq '.my.prefix[] | .primary_key + ":", (.sub.prefix[] | " - " + .sub_key)' | tr -d '"'
Run Code Online (Sandbox Code Playgroud)
其输出与 yaml 足够接近,我通常可以将其导入其他工具而不会出现太大问题。(我仍在寻找一种方法来基本导出输入 json 的子集)
| 归档时间: |
|
| 查看次数: |
140220 次 |
| 最近记录: |