-1

I have a json file GroupList.json:

{
  "results": [
    {
      "type": "known",
      "username": "USERID1",
      "displayName": "DISPLAYNAME1",
      "_links": {
        "self": "https://confluence.com/rest/api/user?key=123"
      },
      "_expandable": {
        "status": ""
      }
    },
    {
      "type": "known",
      "username": "USERID2",
      "displayName": "DISPLAYNAME2",
      "_links": {
        "self": "https://confluence.com/rest/api/user?key=1234"
      },
      "_expandable": {
        "status": ""
      }
    }
  ],
  "start": 0,
  "limit": 50000,
  "size": 2,
  "_links": {
    "self": "https://confluence.com/rest/api/group/GROUPNAME/member",
    "base": "https://confluence.com"
  }
}

I'm fetching the username using the below command: jq -r '.results | map(.username) | join(",")' GroupList.json > UserNameList.txt

Output in UserNameList.txt is: USERID1,USERID2

I want to modify the output to be:

SPACEKEY : GROUPNAME : USERID1,USERID2

SPACEKEY & GROUPNAME are 2 variables whose value changes in every iteration.

peak
  • 105,803
  • 17
  • 152
  • 177
sheerak
  • 13
  • 1
  • 3
  • Did you try using --arg option ? --arg name value – rootkonda Jul 20 '20 at 10:50
  • I did try --arg, but unable to formulate the command to achieve my requirement – sheerak Jul 20 '20 at 11:08
  • jq -r --arg foo "123" '.results | map($foo+.username) | join(",")' file.txt > users.txt - This works as I tested it. When you say each iteration then spacekey and groupname will appear for each record within the results collection. Is that what you want ? then perhaps above style be fine ? – rootkonda Jul 20 '20 at 11:50
  • As you mentioned spacekey and groupname is appearing for each record. I do not want them for each record. As of now it is like "spacekey groupname userid1" "spacekey groupname userid2" I want it to be: "spacekey groupname userid1,userid2" – sheerak Jul 20 '20 at 12:47

1 Answers1

3

With your file, the following invocation produces the output shown below:

jq -r --arg prefix "SPACEKEY : GROUPNAME : " '
   $prefix + (.results | map(.username) | join(","))
' GroupList.json

Output

SPACEKEY : GROUPNAME : USERID1,USERID2
peak
  • 105,803
  • 17
  • 152
  • 177