给定此输入小样本:
{
"_format_version": "1.1",
"_workspace": "test",
"services": [
{
"connect_timeout": 60000,
"host": "host-name-test.com",
"name": "name-of-service",
"path": "/test/oauthpass",
"port": 777,
"protocol": "http",
"read_timeout": 1000,
"retries": 1,
"write_timeout": 1000,
"routes": [
{
"hosts": [
"Google.com"
],
"name": "com.testing.active.oauth",
"methods": [
"POST"
],
"paths": [
"/vendors/otest/pass/?$"
],
"path_handling": "v8",
"preserve_host": false,
"protocols": [
"https"
],
"regex_priority": 0,
"strip_path": true,
"https_redirect_status_code": 426,
"request_buffering": true,
"response_buffering": true
}
]
}
}
试图从数据中获取一个列表,并提取某些值,如下所示:
host-name-test.com, Google.com, POST, HTTPS
到目前为止,我执行的命令是
cat /tmp/petecar.json | jq -r ' .services[] | .routes[] | ( .hosts[] + "/" + .paths[]) ' | more
但是我无法访问服务下的值,请提供一些如何获取值的示例
发布于 2022-08-14 01:10:50
routes
有一个数组值,因此不能与字符串连接。可以使用join
将该数组转换为单个字符串:
jq -r '.services[] | .host + " " + (.routes[].hosts | join(","))'
输出:
host-name-test.com Google.com
或者,使用串内插自动将任何值序列化到其字符串表示形式中:
jq -r '.services[] | "\(.host) \(.routes[].hosts)"'
输出:
host-name-test.com ["Google.com"]
可以组合join
和string插值,给出第一个命令的相同输出:
jq -r '.services[] | "\(.host) \(.routes[].hosts|join(","))"'
https://stackoverflow.com/questions/72129329
复制相似问题