title: Conditionally changing JSON values in jq with sub() function
body: <p>I need to alter some values in JSON data, and would like to include it in an already existing shell script. I'm trying to do so using jq, and will need the "sub()" function to cut off a piece of a string value.</p>
  
  <p>Using this command line:</p>
  
  <pre><code>jq '._meta[][].ansible_ssh_pass | sub(" .*" ; "")'
  </code></pre>
  
  <p>with the data below will correctly replace the value (cutting off anything including the first space in the data), but only prints out the value, not the complete JSON structure.</p>
  
  <p>Here's sample JSON data:</p>
  
  <pre><code>{_meta": {
      "hostvars": {
        "10.1.1.3": {
          "hostname": "core-gw1",
          "ansible_user": "",
          "ansible_ssh_pass": "test123 / ena: test2",
          "configsicherung": "true",
          "os": "ios",
          "managementpaket": ""
        }
      }
  }}
  </code></pre>
  
  <p>Output should be something like this:</p>
  
  <pre><code>{"_meta": {
      "hostvars": {
        "10.1.1.3": {
          "hostname": "core-gw1",
          "ansible_user": "",
          "ansible_ssh_pass": "test123",
          "configsicherung": "true",
          "os": "ios",
          "managementpaket": ""
        }
      }
  }}
  </code></pre>
  
  <p>I assume I have to add some sort of "if... then" based arguments, but haven't been able to get jq to understand me ;) Manual is a bit sketchy and I haven't been able to find any example I could get to match up with what I need to do ...</p>