jq |基于匹配的数组元素返回数据子集

时间:2018-03-07 23:26:29

标签: jq

我正在尝试使用jq解析下面的json文件。我想返回该数据的子集。

  {
      "imageDetails": [
        {
          "imageSizeInBytes": 2066961,
          "imageDigest": "sha256:ff089cca12407adce49e70771a32c2fd48d53d8d3fb735cee430037e6b27c535123",
          "imageTags": [
            "test",
            "latest"
          ],
          "registryId": "98654010662132",
          "repositoryName": "testrepository",
          "imagePushedAt": 1520462286
        },
        {
          "imageSizeInBytes": 2066961,
          "imageDigest": "sha256:c30974de3423250382f5b33be01103c11de73459ceaedd789dff9b461532eb2dd55",
          "imageTags": [
            "test3"
          ],
          "registryId": "98654010662132",
          "repositoryName": "testrepository",
          "imagePushedAt": 1520462305
        },
        {
          "imageSizeInBytes": 2066961,
          "imageDigest": "sha256:05048eb6cc559e953e6c98afa0e71fd836c37a34563531487af2b5d1c9430570ff9",
          "imageTags": [
            "mytest1",
            "mytest2",
            "mytest3"
          ],
          "registryId": "98654010662132",
          "repositoryName": "testrepository",
          "imagePushedAt": 1520459886
        },
        {
          "imageSizeInBytes": 2066961,
          "imageDigest": "sha256:bf23ada8a14d1c70c476b4f39b9b849fd5191d8fc419312b3d1b8f59aa182b050e6",
          "imageTags": [
            "test3-latest"
          ],
          "registryId": "98654010662132",
          "repositoryName": "testrepository",
          "imagePushedAt": 1520462305
        },
        {
          "imageSizeInBytes": 2066961,
          "imageDigest": "sha256:a040f335556e783930905d0b7bd9f55f1400a2778e39983c3d1e3cc41643c090ee0",
          "imageTags": [
            "test2"
          ],
          "registryId": "98654010662132",
          "repositoryName": "testrepository",
          "imagePushedAt": 1520462304
        }
      ]
    }

具体来说,我想搜索“imageTags”,其中数组中的项目是“mytest1”。返回输出应该是字典块。像这样:

    {
      "imageSizeInBytes": 2066961,
      "imageDigest": "sha256:05048eb6cc559e953e6c98afa0e71fd836c37a34563531487af2b5d1c9430570ff9",
      "imageTags": [
        "mytest1",
        "mytest2",
        "mytest3"
      ],
      "registryId": "98654010662132",
      "repositoryName": "testrepository",
      "imagePushedAt": 1520459886
    }

非常感谢任何协助。

1 个答案:

答案 0 :(得分:0)

简单制定查询的关键是any/2

.imageDetails[] | select(any(.imageTags[]; . == "mytest1"))