Im trying to add a variable while getting the info from a json file as shown below.
stack=$(cat profiles.json | jq '.generic.category')
email=$(cat profiles.json | jq '.central.[Need to add $stack variable here].email')
echo $email
password=$(cat profiles.json | jq '.central.[Need to add $stack variable here].password')
echo $password
I tried few things like jq --arg v $stack '.central[$v]password ' but it didnt work.
This is how my the profiles.json looks like:
"central": {
"one": {
"tenant": "xxx-yyy-zzz",
"email": "xyz@gmail.com",
"password": "1111"
},
"two": {
"tenant": "aaa-bbb-ccc",
"email": "xyz@gmail.com",
"password": "2222"
}
},
"generic": {
"username": "root",
"password": "xyz",
"project": "ABC",
"category": "two"
}
What is the right command to fetch the required information using the variable.
$ cat profiles.json
{
"central": {
"one": {
"tenant": "xxx-yyy-zzz",
"email": "xyz@gmail.com",
"password": "1111"
},
"two": {
"tenant": "aaa-bbb-ccc",
"email": "xyz@gmail.com",
"password": "2222"
}
},
"generic": {
"username": "root",
"password": "xyz",
"project": "ABC",
"category": "two"
}
}
$ jq --arg MYVAR one '.central[$MYVAR].password' profiles.json
"1111"
You can do without the shell variable stack
, unless you want to use it in different places later in your shell script.
email=$(jq -r '.central[.generic.category].email' profiles.json)
echo $email
password=$(jq -r '.central[.generic.category].password' profiles.json)
echo $password
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.