Home > Back-end >  jq find a key name in multi-map structured JSON
jq find a key name in multi-map structured JSON

Time:09-02

I have this JSON object that maps the compatibility of patch sets to specific software versions.

{
   "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"
      ]
   }
}

Would it be possible for jq to return the patch set name (e.g. "v7.15.x") when given the specific version (e.g. "8.1.0")?

CodePudding user response:

Yes, it's pretty straightforward actually.

.compatibility | to_entries[] | select(.value | index("8.1.0")) .key

Online demo

In case it's unclear, the version string doesn't have to be hardcoded; it may come from outside the program too:

jq --arg version 8.1.0 '.compatibility | to_entries[] | select(.value | index($version)) .key'
  • Related