我想在JSON文件中找到一个特定的值(不用担心键)在我的JSON文件的任何级别(我事先不知道确切的树),并显示父键。
我提取了这个样本:
cat /tmp/test.json | jq
{
"totalCount": 2,
"pageSize": 1000,
"auditLogs": [
{
"logId": "169807591200060002",
"eventType": "CREATE",
"category": "CONFIG",
"entityId": "HTTP_CHECK-12121212121212",
"timestamp": 1698075912003,
"success": true,
"patch": [
{
"op": "replace",
"path": "/",
"value": {
"steps": [
{
"id": {
"type": "HTTP_CHECK_STEP"
},
"requestType": "OAUTH2",
"destinationUrl": "https://www.mywebsite.com",
"httpMethod": "POST",
"acceptAnyCertificate": true,
"followRedirects": true,
"displayName": "My Website",
"userAgent": "",
"httpCheckHeaders": [
{
"name": "Content-Type",
"value": "application/x-www-form-urlencoded"
}
],
"stepPerformanceThreshold": 0,
"requestBody": null,
"constraints": [
{
"constraintType": "HttpStatusesList",
"passIfFound": false,
"pattern": ">=400"
}
],
"preScript": "",
"postScript": "",
"attributes": {
"oAuth2RequestId": "1",
"oAuth2BodyInputType": "RAW",
"oAuth2addAuthDataTo": "REQUEST_BODY"
},
"postExecutionScriptVariables": [
"{bearerToken-1}"
],
"preExecutionScriptVariables": [],
"certificateId": "",
"basicAuthId": "",
"certStoreId": 0,
"basicAuthStoreId": 0,
"authenticationConfig": {
"type": "BASIC_AUTHENTICATION",
"realmName": null,
"kdcIp": null,
"credentialId": "CREDENTIALS_VAULT-1212121212121"
},
"executionProperties": {},
"shouldNotPersistSensitiveData": true
}
],
"publicLocationIds": [
124
],
"userModificationTimestamp": 1698075911987,
"customProperties": [],
"version": 6
},
"oldValue": null
}
]
},
{
"logId": "169807591200060001",
"eventType": "CREATE",
"category": "CONFIG",
"entityId": "HTTP_CHECK-12121212121212",
"environmentId": "b416bf43-a9d2-4123-aa70-e36ff39c0ad9",
"timestamp": 1698075911986,
"success": true,
"patch": [
{
"op": "replace",
"path": "/",
"value": {
"frequency (Frequency)": 0,
"locations": [
{
"location (Location)": "SYNTHETIC_LOCATION-000000000000007C"
}
]
},
"oldValue": null
}
]
}
]
}
字符串
我可以用这个递归命令找到字符串“CREDENTIALS_VALENT-12121212121”:
cat /tmp/test.json | jq '.auditLogs[] | .. | .credentialId? | select(. == "CREDENTIALS_VAULT-1212121212121")'
"CREDENTIALS_VAULT-1212121212121"
型
但是我也想在第一层获取父键“logId”。我试图使用一个变量($parent),但不幸的是我得到了几个结果,而不是只有一个:
cat /tmp/test.json | jq '.auditLogs[] as $parent | .. | .credentialId? | select(. == "CREDENTIALS_VAULT-1212121212121") | $parent | {"logId":.logId}'
{
"logId": "169807591200060002"
}
{
"logId": "169807591200060001"
}
型
有没有人对这种需求有想法?
4条答案
按热度按时间7gs2gvoe1#
你可以使用
paths
来查找具有匹配值的路径,从中提取前两个项(在本例中为["auditLogs",0]
),使用getpath
来检索该对象,然后从中提取.logId
:个字符
Demo
如果您希望对象只包含
logId
字段,请改用{logId}
:的字符串
Demo的
您也可以考虑通过将
[…]
Package 在过滤器周围来将结果收集在数组中。或者使用--arg
选项从命令行导入搜索关键字,等等。brccelvz2#
。不用担心钥匙
我事先不知道
这里有一个简单的方法,可以满足这两个要求(我敢打赌,它比使用
paths
和getpath
更快):字符串
由于字符串可能出现多次,您可能需要考虑变体,例如将上面的倒数第二行替换为:
型
iq0todco3#
您可以使用
IN
(或any(stream; condition)
):字符串
或者,根据您的输出格式要求:
型
68de4m5k4#
非常感谢您的回答.我终于选择使用第一个解决方案使用getpath()函数.与此解决方案,我们不关心的关键字的名称.事实上,在我的情况下,搜索模式(“CREDENTIALS_VANOW-12121212121”)是不必要的关键字的值“credentialId”.这可以是另一个键:
字符串
这两个条目都被找到了。这对我来说更合适;)但再次感谢每一个人。