Using jq to fetch key-value pairs from a json file

Viewed 78

I am trying to get key@value pairs of JSON file below using jq

{
  "STUFF_RELATED1": "STUFF_RELATED1",
  "STUFF_RELATED2": "STUFF_RELATED2",
  "THINGS": {
    "THING_2": {
      "details": {
        "stuff_branch": "user/dev"
      },
      "repository": "path/to/repo",
      "branch": "master",
      "revision": "dsfkes4s34jlis4jsj4lis4sli3"
    },
    "THING_1": {
      "details": {
        "stuff_branch": "master"
      },
      "repository": "path/to/repo",
      "branch": "master",
      "revision": "dsfkes4s34jlis4jsj4lis4sli3"
    }
  },
  "STUFF": {
    "revision": "4u324i324iy32g",
    "branch": "master"
  }
}

The key@value pair should look like this:

THING_1@dsfkes4s34jlis4jsj4lis4sli3

Currently I have tried this on my own:

jq -r ' .THINGS | keys[] as $k | "($k)@(.[$k].revision)" ' things.json

But it does not give the resul that I really want.:( Thanks in advance!

1 Answers

You need to escape ( :

jq -r ' .THINGS | keys[] as $k | "\($k)@\(.[$k].revision)" ' things.json
Related