title: JQ - Append json data before and after
body: <p>I want to append some keys and values and object before the jq output and then at the end append auth and ID. How can I do that?</p>
  
  <p>For Case 1 - At the beginning  I want to append this</p>
  
  <pre><code>{
      "jsonrpc": "2.0",
      "method": "host.get",
      "params":
          "filter": {
  </code></pre>
  
  <p>and at the end the following.</p>
  
  <pre><code>},
      "auth": "038e1d7b1735c6a5436ee9eae095879e",
      "id": 1
  }
  </code></pre>
  
  <p>Any suggestions? </p>
  
  <p>Case 1:</p>
  
  <pre><code>echo $hosts
  host001 host002 host003
  
  echo $hosts | jq -R 'split (" ")' | jq -s '{hosts:map({"hostid":.})}'
  {
    "hosts": [
      {
        "hostid": [
          "host001",
          "host002",
          "host003"
        ]
      }
    ]
  }
  </code></pre>
  
  <p>But I want to output like this which includes jsonrpc, methods, params, filter and then the host array needs to be added and in the end append auth and id.</p>
  
  <p>Expected Query 1:</p>
  
  <pre><code>{
      "jsonrpc": "2.0",
      "method": "host.get",
      "params": {
          "filter": {
              "hostid": [
                  "host001",
                  "host002",
                  "host003"
              ]
          }
      },
      "auth": "038e1d7b1735c6a5436ee9eae095879e",
      "id": 1
  }
  </code></pre>
  
  <p>Case 2:</p>
  
  <p>I want to append the jsonrpc, methods params etc, but also append <code>$hostid_json</code> after <code>$groupid_json</code> and then append auth, id etc., as show in query 2.</p>
  
  <pre><code>echo $group_id
  92 93
  
  groupid_json=$(echo $group_id | jq -R 'split (" ")'  | jq '.[]'  | jq -s '{groups:map({"groupid":.})}')
  { "groups": [ { "groupid": "92" }, { "groupid": "93" } ] }
  
  echo $hosts_id
  1111 2222
  
  hostid_json=$(echo $hosts_id | jq -R 'split (" ")'  | jq '.[]'  | jq -s '{hosts:map({"hostid":.})}')
  { "hosts": [ { "hostid": "1111" }, { "hostid": "2222" } ] }
  </code></pre>
  
  <p>But the final ouput I need is like the following.</p>
  
  <p>Expected Query 2:</p>
  
  <pre><code>{
      "jsonrpc": "2.0",
      "method": "hostgroup.massadd",
      "params": {
          "groups": [
              {
                  "groupid": "92"
              },
              {
                  "groupid": "93"
              }
          ],
          "hosts": [
              {
                  "hostid": "1111"
              },
              {
                  "hostid": "2222"
              }
          ]
      },
      "auth": "f223adf833b2bf2ff38574a67bba6372",
      "id": 1
  }
  </code></pre>