jq -在json文件的任何级别搜索字符串值并获取父键

s5a0g9ez  于 2023-11-20  发布在  其他
关注(0)|答案(4)|浏览(120)

我想在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"
}


有没有人对这种需求有想法?

7gs2gvoe

7gs2gvoe1#

你可以使用paths来查找具有匹配值的路径,从中提取前两个项(在本例中为["auditLogs",0]),使用getpath来检索该对象,然后从中提取.logId

jq -r 'getpath(paths(. == "CREDENTIALS_VAULT-1212121212121")[:2]).logId'

个字符
Demo
如果您希望对象只包含logId字段,请改用{logId}

jq 'getpath(paths(. == "CREDENTIALS_VAULT-1212121212121")[:2]) | {logId}'
{
  "logId": "169807591200060002"
}

的字符串
Demo
您也可以考虑通过将[…] Package 在过滤器周围来将结果收集在数组中。或者使用--arg选项从命令行导入搜索关键字,等等。

brccelvz

brccelvz2#

。不用担心钥匙
我事先不知道
这里有一个简单的方法,可以满足这两个要求(我敢打赌,它比使用pathsgetpath更快):

.. | objects
| select(.logId)
| .logId as $id
| .. | objects
| select(.[] == "CREDENTIALS_VAULT-1212121212121")
| $id

字符串
由于字符串可能出现多次,您可能需要考虑变体,例如将上面的倒数第二行替换为:

| select(first(.[] == "CREDENTIALS_VAULT-1212121212121" // empty))

iq0todco

iq0todco3#

您可以使用IN(或any(stream; condition)):

.auditLogs[]
| select(IN(.. | .credentialId?; "CREDENTIALS_VAULT-1212121212121"))
| .logId

字符串
或者,根据您的输出格式要求:

.auditLogs[]
| select(IN(.. | .credentialId?; "CREDENTIALS_VAULT-1212121212121"))
| { logId }

68de4m5k

68de4m5k4#

非常感谢您的回答.我终于选择使用第一个解决方案使用getpath()函数.与此解决方案,我们不关心的关键字的名称.事实上,在我的情况下,搜索模式(“CREDENTIALS_VANOW-12121212121”)是不必要的关键字的值“credentialId”.这可以是另一个键:

$ grep "CREDENTIALS_VAULT-1212121212121" auditlogs2.json
                  "credentialId": "CREDENTIALS_VAULT-1212121212121"
                  "myotherkey": "CREDENTIALS_VAULT-1212121212121"

$ cat auditlogs2.json | jq -r 'getpath(paths(. == "CREDENTIALS_VAULT-1212121212121")[:2]) | .logId'
169807591200060002
169807591200060001

字符串
这两个条目都被找到了。这对我来说更合适;)但再次感谢每一个人。

相关问题