I have to query a jason file to retrieve the value of service\_name, then take the value, remove the double quotes, convert spaces to dashes, and then uppercase to lower case.

Here is my json production.json file

```
{
    "port": 5000,
    "machine": "102",
    "ip": "xxx.xxx.xxx.xxx",
    "drum_id": "1305145216186552",
    "client": {
        "service_name": "Two Men And A Truck",
        "vendor": "default"
    }
}

```

and this is my command

`jq '"\(.client.service_name)"' /home/systems/clients/000002/production.json | sed 's/"//g;s/ /-/g;s/\(.*\)/\L\1/'`

and this works just fine.

```
root@0200 ~ # cat /home/systems/clients/000002/production.json
{
    "port": 5000,
    "machine": "102",
    "ip": "xxx.xxx.xxx.xxx",
    "drum_id": "1305145216186552",
    "client": {
        "service_name": "Two Men And A Truck",
        "vendor": "default"
    }
}
root@0200 ~ # jq '"\(.client.service_name)"' /home/systems/clients/000002/production.json | sed 's/"//g;s/ /-/g;s/\(.*\)/\L\1/'
two-men-and-a-truck

```

Now here is my initial script. It's all going vlan atm and I'll eventually run it through an ssh tunnel.

```
#!/bin/bash
#

#-- tmp files
tmp_dir="$(mktemp -d -t 'text.XXXXX' || mktemp -d 2>/dev/null)"
tmp_input1="${tmp_dir}/temp_input1.txt"
tmp_input2="${tmp_dir}/temp_input2.txt"
tmp_input3="${tmp_dir}/temp_input3.txt"

#- servers
cbservers=( "xxx.xxx.xxx.xxx" "xxx.xxx.xxx.xxx" )

for cbserver in "${cbservers[*]}"; do
  ssh user@"$cbserver" "ls /home/systems/clients | grep '^[0-9]'" > "$tmp_input1"
  while read client; do
    ssh user@"$cbserver" "jq '"\(.client.service_name)"' /home/systems/clients/000002/production.json | sed 's/"//g;s/ /-/g;s/\(.*\)/\L\1/'" > "$tmp_input3"
  done<"$tmp_input1"
done

rm -rf "$tmp_dir"

```

I am having a hell of a time trying to wrap the command being sent over ssh.

`ssh user@"$cbserver" "jq '"\(.client.service_name)"' /home/systems/clients/000002/production.json | sed 's/"//g;s/ /-/g;s/\(.*\)/\L\1/'" > "$tmp_input3"`

If there is an easier way to do this, please enlighten me.

**Update**

I followed everyone's advice below

```
#!/bin/bash
#

#-- tmp files
tmp_dir="$(mktemp -d -t 'text.XXXXX' || mktemp -d 2>/dev/null)"
tmp_input1="${tmp_dir}/temp_input1.txt"
tmp_input2="${tmp_dir}/temp_input2.txt"

#- servers
cbservers=( "xxx.xxx.xxx.xxx" "xxx.xxx.xxx.xxx" )

for cbserver in "${cbservers[*]}"; do
  ssh user@"$cbserver" "ls /home/systems/clients | grep '^[0-9]'" > "$tmp_input1"
  while read client; do
    file="/home/systems/clients/${client}/production.json "
    ssh user@"$cbserver" jq --raw-output '.client.service_name' "$file" | sed 's/ /-/g;s/\(.*\)/\L\1/' > "$tmp_input2"
  done<"$tmp_input1"
done

rm -rf "$tmp_dir"

```

the "while read client" loop only runs once and it should run 12 times.