JMeshPath 多种条件过滤器

问题描述 投票:0回答:1

我有这个数据集

{
"imageDetails": [
    {
        "registryId": "259611133533",
        "repositoryName": "core",
        "imageDigest": "sha256:3585b2754d8429cef04909b97dcc1bf5eacb74bf1c18718a3d602fbc639decf5",
        "imageTags": [
            "DEV16419adddockerpullforimageused-202404241351"
        ],
        "imageSizeInBytes": 471224049,
        "imagePushedAt": "2024-04-24T15:56:39+02:00",
        "imageManifestMediaType": "application/vnd.docker.distribution.manifest.v2+json",
        "artifactMediaType": "application/vnd.docker.container.image.v1+json"
    },
    {
        "registryId": "259611133533",
        "repositoryName": "core",
        "imageDigest": "sha256:b17e3f35ff2e0b9af437372d7777e1cc092994eb87dbddf2d73d467eb122cf51",
        "imageTags": [
            "master-202404300732"
        ],
        "imageSizeInBytes": 470904068,
        "imagePushedAt": "2024-04-30T09:34:19+02:00",
        "imageManifestMediaType": "application/vnd.docker.distribution.manifest.v2+json",
        "artifactMediaType": "application/vnd.docker.container.image.v1+json"
    },
    {
        "registryId": "259611133533",
        "repositoryName": "core",
        "imageDigest": "sha256:dfe2a1df2d6d8b39bdd37887979f027de29f802295e1b7536c31ac786fc0bbea",
        "imageTags": [
            "develop-202404230503"
        ],
        "imageSizeInBytes": 471216957,
        "imagePushedAt": "2024-04-23T07:07:14+02:00",
        "imageManifestMediaType": "application/vnd.docker.distribution.manifest.v2+json",
        "artifactMediaType": "application/vnd.docker.container.image.v1+json",
        "lastRecordedPullTime": "2024-04-24T08:55:56.071000+02:00"
    },
    {
        "registryId": "259611133533",
        "repositoryName": "core",
        "imageDigest": "sha256:da82d9236a5bff64a994aee120a3c2be72601e7f36db1581cc6a009e12fc39ca",
        "imageTags": [
            "develop-202404260502"
        ],
        "imageSizeInBytes": 471225444,
        "imagePushedAt": "2024-04-26T07:03:58+02:00",
        "imageManifestMediaType": "application/vnd.docker.distribution.manifest.v2+json",
        "artifactMediaType": "application/vnd.docker.container.image.v1+json",
        "lastRecordedPullTime": "2024-04-27T06:30:55.055000+02:00"
    },
    {
        "registryId": "259611133533",
        "repositoryName": "core",
        "imageDigest": "sha256:39239ba86a543aa6787da0a02479d6f59eb00742e4810a9d4b52f1423a5e0392",
        "imageTags": [
            "master-202404260911"
        ],
        "imageSizeInBytes": 470843809,
        "imagePushedAt": "2024-04-26T11:12:51+02:00",
        "imageManifestMediaType": "application/vnd.docker.distribution.manifest.v2+json",
        "artifactMediaType": "application/vnd.docker.container.image.v1+json",
        "lastRecordedPullTime": "2024-04-26T11:14:06.695000+02:00"
    }
]}

我想通过 imagePushedAt 和 imageTags 过滤数据集,其中应包含字符串 master。
我已经测试了以下内容

imageDetails[?imagePushedAt<`2024-04-27` && imageDetails.contains(@, `master`)]

但不幸的是这不起作用。 我在某种程度上通过使用这个查询得到了我想要的东西

imageDetails[?imagePushedAt < `2024-04-27`] | [].imageTags[?contains(@,`master`)]

但是当 imageTags 不匹配时,此解决方案返回空数组,而不是过滤掉结果

jmespath
1个回答
0
投票

由于

imageTags
是一个数组,
imageTags.contains(@, `master`)
意味着您希望在
master
数组中严格等于
imageTags
的元素,而您的意思是 ”对于数组的每个元素,我都会执行字符串包含
master

后者可以通过

表示
imageTags[?contains(@, `master`)]

因为,虽然

@
应用于数组时代表数组的一个元素,但这里它被 作为过滤器应用于数组的每个元素,因此
@
现在代表
imageTags 的每个字符串
数组。

所以,你的整个查询最终是:

imageDetails[?
  imagePushedAt<`2024-04-27` 
  && imageTags[?contains(@, `master`)]
]

在您的示例 JSON 中,这将产生单个元素:

[
  {
    "registryId": "259611133533",
    "repositoryName": "core",
    "imageDigest": "sha256:39239ba86a543aa6787da0a02479d6f59eb00742e4810a9d4b52f1423a5e0392",
    "imageTags": [
      "master-202404260911"
    ],
    "imageSizeInBytes": 470843809,
    "imagePushedAt": "2024-04-26T11:12:51+02:00",
    "imageManifestMediaType": "application/vnd.docker.distribution.manifest.v2+json",
    "artifactMediaType": "application/vnd.docker.container.image.v1+json",
    "lastRecordedPullTime": "2024-04-26T11:14:06.695000+02:00"
  }
]
© www.soinside.com 2019 - 2024. All rights reserved.