title: Add a Key Value Pair in JSON Object Using JQ and Bash
body: <p>Editing this post for clarification as I didn't do a good job the first time around --</p>
  
  <p>I have a script that crawls through a directory and pulls info from WP sites and builds a JSON array of objects that will eventually be fed into a visual dashboard. The script works perfectly but I can't seem to get the <code>siteurl</code> key to post in every object as it does in the first.</p>
  
  <p>The key-pairs that have null need to be replaced with the corresponding <code>siteurl</code> within it's array. </p>
  
  <p>I feel I should be able to do this with jq and the <code>--arg name value</code> option but I clearly am doing something wrong. Not sure if I'm not passing the variables correctly or if it's an issue with WPCLI.</p>
  
  <p>Output and script are below.</p>
  
  <pre><code>[
    {
      "eventType": "WordpressSite",
      "siteurl": "http://mytest1.com"
    },
    {
      "eventType": "WordpressPlugin",
      "plugin_name": "akismet",
      "plugin_status": "inactive",
      "plugin_update": "available",
      "plugin_version": "4.0.8",
      "siteurl": null
    },
    {
      "eventType": "WordpressPlugin",
      "plugin_name": "hello",
      "plugin_status": "inactive",
      "plugin_update": "none",
      "plugin_version": "1.7",
      "siteurl": null
    },
    {
      "eventType": "WordpressUser",
      "siteurl": null,
      "user_email": "example.user@email.com",
      "user_name": "testuser1",
      "user_registered": "2018-11-26 17:44:09",
      "user_role": "administrator"
    }
  ]
  [
    {
      "eventType": "WordpressSite",
      "siteurl": "http://mytest2.com"
    },
    {
      "eventType": "WordpressPlugin",
      "plugin_name": "akismet",
      "plugin_status": "inactive",
      "plugin_update": "available",
      "plugin_version": "4.0.8",
      "siteurl": null
    },
    {
      "eventType": "WordpressPlugin",
      "plugin_name": "hello",
      "plugin_status": "inactive",
      "plugin_update": "none",
      "plugin_version": "1.7",
      "siteurl": null
    },
    {
      "eventType": "WordpressUser",
      "siteurl": null,
      "user_email": "example.user@email.com",
      "user_name": "testuser2",
      "user_registered": "2018-11-26 17:44:04",
      "user_role": "administrator"
    }
  ]
  </code></pre>
  
  <hr>
  
  <pre><code>#!/bin/bash
  
  #for d in /var/www/* ; do
  #   echo "$d"
  #done
  
  for f in /var/www/*/public_html/; do
      if [[ -d $f ]]; then
                  #echo "$f"
  
                  cd $f;
                  siteurl=$(wp option get siteurl)
                  users=$(wp user list --fields=display_name,user_email,user_registered,roles --format=json | jq '.[] | .eventType = "WordpressUser"' | jq . --arg siteurl $(wp option get siteurl) | jq '. + {"user_name": .display_name, "user_role": .roles, "siteurl": ."$siteurl"} | del (.display_name, .roles)')
                  plugins=$(wp plugin list --format=json | jq '.[] | .eventType = "WordpressPlugin"' | jq '. + {"plugin_name": .name, "plugin_status": .status, "plugin_version": .version, "plugin_update": .update, "siteurl": ."$siteurl"} | del(.name, .status, .version, .update)')
  
  
                  {
                  printf '{"eventType":"WordpressSite","siteurl":"%s"}\n' "$(wp option get siteurl)"
                  echo "$plugins"
                  echo "$users"
                  } | jq -sS .
                  cd ../..;
      fi
  done
  </code></pre>