title: Update values in json with jq (shell script)
body: <p>I am trying to update a couple values in a json array (separate file) with a shell script. Basically the logic is, set an environment variable called URL, ping that URL and add it to the json-- if 0, update another json field to SUCCESS, else update to FAILED.</p>
  
  <p>Here is are the files:</p>
  
  <p>info.json:</p>
  
  <pre><code>{
   "name": "PingTest",",
   "metrics": [
  {
    "event_type": "PingResult",
    "provider": "test",
    "providerUrl": "URL",
    "providerResult": "RESULT"
  }
  ]
  }
  </code></pre>
  
  <p>pinger.sh:</p>
  
  <pre><code>#!/bin/sh
  
  JSON=`cat info.json` #read in JSON
  
  #Assume URL variable is set to www.dksmfdkf.com 
  ping -q -c 1 "$URL" &gt; /dev/null #ping url
  if [ $? -eq 0 ]; then #if ping success, replace result in json template
    JSON=`echo ${JSON} | jq --arg v "$URL" '.metrics[].providerUrl |= $v' 
    info.json`
    JSON=`echo ${JSON} | jq '.metrics[].providerResult |= "SUCCESS"' info.json`
  else
    JSON=`echo ${JSON} | jq --arg v "$URL" '.metrics[].providerUrl |= $v' 
    info.json`
    JSON=`echo ${JSON} | jq '.metrics[].providerResult |= "FAILED"' info.json`
  fi
  
  #Remove whitespace from json
  JSON=`echo $JSON | tr -d ' \t\n\r\f'`
  
  #Print the result
  echo "$JSON"
  </code></pre>
  
  <p>The problem is my json file isn't getting updated properly, example result when running:</p>
  
  <pre><code>home:InfraPingExtension home$ ./pinger.sh
  ping: cannot resolve : Unknown host
  {
    "name": "PingTest",
    "metrics": [
    {
      "event_type": "PingResult",
      "provider": "test",
      "providerUrl": "",
      "providerResult": "RESULT"
    }
   ]
   }
  {
    "name": "PingTest",
    "metrics": [
  {
    "event_type": "PingResult",
    "provider": "test",
    "providerUrl": "URL",
    "providerResult": "FAILED"
  }
    ]
  }
  
  {"name":"PingTest","metrics":[{"event_type":"PingResult","provider":"test","providerUrl":"URL","providerResult":"RESULT"}]}
  </code></pre>