jq 在多映射结构化 JSON 中查找键名

ssc*_*zio -1 json jq

我有这个 JSON 对象,它将补丁集的兼容性映射到特定的软件版本。

{
   "compatibility":{
      "v7.11.x":[
         "7.12.1",
         "7.12.0",
         "7.11.2",
         "7.11.1",
         "7.11.0"
      ],
      "v7.13.x":[
         "7.14.2",
         "7.14.1",
         "7.14.0",
         "7.13.1",
         "7.13.0"
      ],
      "v7.15.x":[
         "8.1.0",
         "8.0.1",
         "8.0.0",
         "7.17.1",
         "7.17.0",
         "7.16.1",
         "7.16.0",
         "7.15.1",
         "7.15.0"
      ]
   }
}
Run Code Online (Sandbox Code Playgroud)

当给定特定版本(例如“8.1.0”)时,是否可以jq返回补丁集名称(例如“v7.15.x”)?

ogu*_*ail 6

是的,实际上非常简单。

.compatibility | to_entries[] | select(.value | index("8.1.0")) .key
Run Code Online (Sandbox Code Playgroud)

在线演示

如果不清楚,版本字符串不必进行硬编码;它也可能来自程序外部:

.compatibility | to_entries[] | select(.value | index("8.1.0")) .key
Run Code Online (Sandbox Code Playgroud)