这是从Extracting selected properties from a nested JSON object with jq开始的,它使那里的OP摆脱了嵌套对象带来的不必要的特性。
我遇到了同样的问题,但是不是以数组开头[,而是拥有一个JSON对象流,每个对象都是这样的:
{
"localHostName" : "rest-2-17ve6",
"port" : "80",
"requestHeaders" : {
"x-forwarded-port" : "443",
"x-forwarded-host" : "dummy.com",
"content-length" : "15959431",
"accept" : "*/*",
"x-forwarded-for" : "10.1.9.11",
"authorization" : "hash is present",
"expect" : "100-continue",
"forwarded" : "for=10.5.9.1;host=dummy.com;proto=https",
"content-type" : "application/json",
"host" : "dummy.com",
"x-forwarded-proto" : "https",
"user-agent" : "curl/7.51.0"
},
"uri" : "/2/data/saveList",
"protocol" : "HTTP/1.1",
"threadName" : "http-nio-8080-exec-10",
"requestBytes" : 15959431,
"applicationDuration" : 44135,
"responseStatus" : "200",
"remoteIpAddress" : "10.1.10.1",
"responseHeaders" : {
"X-XSS-Protection" : "1; mode=block",
"Content-Type" : "application/json;charset=UTF-8",
"X-Content-Type-Options" : "nosniff",
"Cache-Control" : "no-cache, no-store, max-age=0, must-revalidate",
"Date" : "Wed, 20 Jun 2018 15:53:27 GMT",
"Transfer-Encoding" : "chunked",
"Vary" : "Accept-Encoding",
"X-Frame-Options" : "DENY",
"Expires" : "0",
"Pragma" : "no-cache"
},
"isoDateTime" : "2018-06-20T15:52:42.466913985Z",
"method" : "POST",
"username" : "rd7y1",
"localIpAddress" : "10.129.9.238",
"responseBytes" : 2,
"requestContentExcerpt" : "blah",
"totalDuration" : 44869,
"responseContentExcerpt" : " [] "
}
我想在命令行上过滤流,所以我只会得到:
{
"isoDateTime" : "2018-06-20T15:52:42.466913985Z",
"method" : "POST",
"username" : "rd7y1",
"requestHeaders.user-agent" : "Rcurl"
}
我尝试了cat /logs/json.log | jq -cC 'map(requestHeaders|={user-agent})'
,但遇到语法错误。
答案 0 :(得分:4)
由于jq是面向流的,因此只使用select(...)
而不是map(select(...))
您似乎打算在选择标准中使用.requestHeaders."user-agent"
。
通常建议尽可能避免使用cat
。
根据您的要求,应该删除-c命令行选项。
由于示例输入中未出现“ Rcurl”,因此我将使用出现的字符串。
因此,在您的情况下,您将得到类似以下的内容:
< /logs/json.log jq '
select(.requestHeaders."user-agent" == "curl/7.51.0")
| {isoDateTime, method, username,
"requestHeaders.user-agent": .requestHeaders."user-agent"}'