Duma Alex
Duma Alex

Reputation: 33

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

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!

Upvotes: 3

Views: 129

Answers (1)

Philippe
Philippe

Reputation: 26377

You need to escape ( :

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

Upvotes: 3

Related Questions