0

I want to get the IPs that has 'server.sh' value. My current script gets all the IPs

test.json

{
  "nodes": {
    "test1.local": {
      ":ip": "192.168.56.30",
      ":server": "server.sh",
      ":client": "client.sh"
    },
    "test2.local": {
      ":ip": "192.168.56.31",
      ":server": "server.sh",
      ":client": "client.sh"
    },
    "test3.local": {
      ":ip": "192.168.56.32",
      ":client": "client.sh"
    }
  }
}

test.sh

ips=`jq -c '.nodes | to_entries | map(.value.":ip")| map_values(.+":4648")' test.json`
echo $ips

["192.168.56.30:4648","192.168.56.31:4648","192.168.56.32:4648"]

rkevx21
  • 2,441
  • 5
  • 19
  • 40
  • 1
    Note that `echo $ips` is innately buggy. *Always* quote your expansions: `echo "$ips"`; see [BashPitfalls #14](http://mywiki.wooledge.org/BashPitfalls#echo_.24foo), and the Stack Overflow question [I just assigned a variable, but `echo $variable` shows something else](https://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else). – Charles Duffy Apr 02 '20 at 02:36
  • 1
    ...and it's strongly preferred to use `ips=$(jq ...)` instead of legacy backtick command-substitution syntax. – Charles Duffy Apr 02 '20 at 02:37

1 Answers1

2

Is it ok for your task?

jq '.nodes|.[]|select(.":server"=="server.sh")|.":ip"+":4648"' test.json 
"192.168.56.30:4648"
"192.168.56.31:4648"
Fedor Dikarev
  • 506
  • 3
  • 9