0

I'm able to retrieve each user's data into m4-$u.json file with the below shell script

#!/bin/bash

USERID=ricardo.sanchez
PASSWORD=password
PORT=2728

for u in `cat user-list.txt`;
   do echo $u; 
   curl --user $USERID:$PASSWORD http://198.98.99.12:46567/$PORT/protects/$u | jq '.' > m4-$u.json
done

One for the user's output of m4-daniel.json file few lines as follows.

[
  {
    "depotFile": "//ABND/JJEB/...",
    "host": "*",
    "isgroup": "",
    "line": "16",
    "perm": "open",
    "user": "5G_USER_GROUP"
  },
  {
    "depotFile": "//LIB/...",
    "host": "*",
    "isgroup": "",
    "line": "19",
    "perm": "write",
    "user": "6G_USER_GROUP"
  },
  {
    "depotFile": "//AND/RIO/...",
    "host": "*",
    "isgroup": "",
    "line": "20",
    "perm": "write",
    "user": "AND_USER_GROUP"
  },

During shell script run time additionally $PORT & $u variable values need to be added in output of each json file.

Expected json output:-

  {
    "depotFile": "//ABND/JJEB/...",
    "host": "*",
    "isgroup": "",
    "line": "16",
    "perm": "open",
    "user": "5G_USER_GROUP",
    "port": "2728",
    "userid": "daniel"
  },

To achieve this any help will be appreciated.

user4948798
  • 1,924
  • 4
  • 43
  • 89
  • You want _merge json array in `jq`_ ? – KamilCuk Aug 05 '21 at 10:23
  • yes correct, These two are input "port": "2728", and "userid": "daniel" in shell script. these need be added in Json file. – user4948798 Aug 05 '21 at 10:29
  • https://stackoverflow.com/questions/42011086/merge-arrays-of-json Does this answer your question? – KamilCuk Aug 05 '21 at 10:31
  • @KamilCuk, `$PORT` and `$u `are input values from shell scrript, these need to be present in json output for each user as mentioned above. – user4948798 Aug 05 '21 at 12:09
  • @Kishore, for getting shell variables into jq so you can use them in expressions there, see [passing bash variable to jq](https://stackoverflow.com/questions/40027395/passing-bash-variable-to-jq) – Charles Duffy Aug 05 '21 at 14:33
  • @Kishore, ...in general, you'll get a better response if you factor out the parts of your question that are already solved elsewhere in the knowledge base, demonstrate knowledge of the things you _do_ know, and focus in on the parts that are new and unique – Charles Duffy Aug 05 '21 at 14:34
  • (btw, it's also possible to let jq directly read your input file, instead of reading it in shell and handing it off to jq). – Charles Duffy Aug 05 '21 at 14:37

1 Answers1

1

By using --arg with jq you can pass paremeter and use as a variables ( more info https://stedolan.github.io/jq/manual/v1.5/#Invokingjq )

by using map and + you can iterate over the array and add a new property for each associative array

in you case :

curl --user $USERID:$PASSWORD http://198.98.99.12:46567/$PORT/protects/$u  \
   | jq --arg a_port $PORT  --arg $u  $USER 'map(.+{"userid":$a_userid}+{"port":$a_port|tonumber})'  > m4-$u.json

see addition and map in https://stedolan.github.io/jq/manual/v1.5/#Builtinoperatorsandfunctions

EchoMike444
  • 1,513
  • 1
  • 9
  • 8