英文:
jq: Remove fields form item array
问题
[
{
"httpRequest": {
"method": "POST",
"path": "/catalog-data-rest/catalog/search.xml",
"body": {
"type": "STRING",
"string": "<request><oid>2.16.840.1.113883.4.292.10.12</oid><startIndex>1</startIndex><pageSize>100</pageSize></request>",
"contentType": "text/html; charset=utf-8"
}
},
"httpResponse": {
"statusCode": 200,
"reasonPhrase": "OK",
"headers": {
"Content-Type": [
"application/xml"
]
},
"body": {
"type": "XML",
"xml": "<?xml version= \"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?> <resultSet> <request> <oid>2.16.840.1.113883.4.292.10.12</oid>..."
"contentType": "application/xml"
}
}
},
{
//...
}
]
英文:
This is my desired output:
[
{
"httpRequest": {
"method": "POST",
"path": "/catalog-data-rest/catalog/search.xml",
"body": {
"type": "STRING",
"string": "<request><oid>2.16.840.1.113883.4.292.10.12</oid><startIndex>1</startIndex><pageSize>100</pageSize></request>",
"contentType": "text/html; charset=utf-8"
}
},
"httpResponse": {
"statusCode": 200,
"reasonPhrase": "OK",
"headers": {
"Content-Type": [
"application/xml"
]
},
"body": {
"type": "XML",
"xml": "<?xml version= \"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?> <resultSet> <request> <oid>2.16.840.1.113883.4.292.10.12</oid>...",
"contentType": "application/xml"
}
}
},
{
//...
}
]
The problem is that my raw source is a bit not clear and I need to clear it up:
[ {
"httpRequest" : {
"method" : "POST",
"path" : "/catalog-data-rest/catalog/search.xml",
"headers" : {
"content-length" : [ "141" ],
"content-encoding" : [ ".*" ],
"User-Agent" : [ "Apache-HttpClient/4.5.13 (Java/11.0.19)" ],
"Host" : [ "preproduccio.gcatalegs.isisscat.intranet.gencat.cat:8443" ],
"Cookie" : [ "JSESSIONID=0000DVEWfJHnChmIF3Qm8fS7AOG:19pp48tgo" ],
"Content-Type" : [ "text/xml;charset=UTF-8" ],
"Connection" : [ "Keep-Alive" ],
"Authorization" : [ "Basic VVNVTVBJVFNUOkJ4c2w3MjU5" ],
"Accept-Encoding" : [ "gzip,deflate" ]
},
"cookies" : {
"JSESSIONID" : "0000DVEWfJHnChmIF3Qm8fS7AOG:19pp48tgo"
},
"keepAlive" : true,
"secure" : true,
"localAddress" : "127.0.0.1:1080",
"remoteAddress" : "127.0.0.1",
"body" : {
"type" : "XML",
"xml" : "<request><tableName>ET 99CCA</tableName><owner><ownerType>REF</ownerType></owner><startIndex>1</startIndex><pageSize>100</pageSize></request>",
"rawBytes" : "PHJlcXVlc3Q+PHRhYmxlTmFtZT5FVCA5OUNDQTwvdGFibGVOYW1lPjxvd25lcj48b3duZXJUeXBlPlJFRjwvb3duZXJUeXBlPjwvb3duZXI+PHN0YXJ0SW5kZXg+MTwvc3RhcnRJbmRleD48cGFnZVNpemU+MTAwPC9wYWdlU2l6ZT48L3JlcXVlc3Q+",
"contentType" : "text/xml; charset=utf-8"
}
},
"httpResponse" : {
"statusCode" : 200,
"reasonPhrase" : "OK",
"headers" : {
"X-XSS-Protection" : [ "1; mode=block" ],
"X-Powered-By" : [ "Servlet/3.0" ],
"X-Frame-Options" : [ "DENY" ],
"X-Content-Type-Options" : [ "nosniff" ],
"Strict-Transport-Security" : [ "max-age=31536000 ; includeSubDomains" ],
"Set-Cookie" : [ "JSESSIONID=0000NOyZg7ww-mcbN2YfxKE-957:19pp48tgo; Path=/; Secure; HttpOnly" ],
"Pragma" : [ "no-cache" ],
"Expires" : [ "0" ],
"Date" : [ "Mon, 17 Jul 2023 15:01:13 GMT" ],
"Content-Type" : [ "application/xml" ],
"Content-Language" : [ "en-US" ],
"Cache-Control" : [ "no-cache, no-store, max-age=0, must-revalidate" ]
},
"cookies" : {
"JSESSIONID" : "0000NOyZg7ww-mcbN2YfxKE-957:19pp48tgo"
},
"body" : {
"type" : "XML",
"xml" : "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><resultSet><request><tableName>ET 99CCA</tableName><owner><ownerType>RE...",
"rawBytes" : "PD94bWwgdmVyc2lvbj0iMS4wIiBlb...",
"contentType" : "application/xml"
}
},
"id" : "8755ddd1-835c-40fe-870f-decf16242e4e",
"priority" : 0,
"timeToLive" : {
"unlimited" : true
},
"times" : {
"remainingTimes" : 1
}
} ]
How could I remove httpRequest.headers
and httpRequest.cookies
from each item in array?
答案1
得分: 2
要删除每个嵌套对象中的所有headers
和cookies
,请使用以下命令:
map(.[] |= del(.headers?, .cookies?))
如果您不想从每个对象中删除它们,而只是想删除其中一些,请列出要删除的每个项:
map(del(.httpRequest.cookies, .httpRequest.headers, .httpResponse.headers, .httpResponse.cookies))
如果您想保留每个.httpRequest
和.httpResponse
的一些硬编码键,可以使用map()
来更新这些键:
map(.httpRequest |= { method } | .httpResponse |= { statusCode })
然后,根据OP的评论,如果您想在.header
内指定一些键,而不使用简写,请定义键,然后使用相同的更新逻辑来保留一些键:
map(.httpRequest |= { method } | .httpResponse |= { headers: (.headers | { Expires }), statusCode })
正如OP的新评论中所要求的那样,要扩展上述过滤器以删除任何其他顶级键,我们可以使用相同的策略,创建一个新对象(这样我们就不需要del
掉一切),但使用与上面相同的逻辑来更新所需的键:
map({
httpRequest: (.httpRequest | { method }),
httpResponse: (.httpResponse | { headers: (.headers | { Expires }), statusCode }),
})
英文:
To delete all headers
and cookies
from every nested object, use
map(.[] |= del(.headers?, .cookies?))
Jqplay Demo
If you don't want to remove them from every object, rather just a few, listing each item in a del
would be enough:
map(del(.httpRequest.cookies, .httpRequest.headers, .httpResponse.headers, .httpResponse.cookies))
JqPlay Demo
If you would like to keep a few hardcoded keys for each .httpRequest
and .httpResponse
, you can map()
over them and them update (|=
) that key:
map(.httpRequest |= { method } | .httpResponse |= { statusCode })
JqPlay Demo
Then, based on OP's comment, if you want to specify a few keys inside .header
, instead of using the short hand, define the key and then use the same update logic to keep a few keys:
map(.httpRequest |= { method } | .httpResponse |= { headers: (.headers | { Expires }), statusCode })
JqPlay Demo
As asked in OP's new comment, on to extend above filter to remove any other top leven keys; We can use the same strategy, create a new object (so we don't need to del
everything), but update the required keys with the same logic as above
map({
httpRequest: (.httpRequest | { method }),
httpResponse: (.httpResponse | { headers: (.headers | { Expires }), statusCode }),
})
JqPlay Demo
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论