0
votes

Editing this post for clarification as I didn't do a good job the first time around --

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 siteurl key to post in every object as it does in the first.

The key-pairs that have null need to be replaced with the corresponding siteurl within it's array.

I feel I should be able to do this with jq and the --arg name value 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.

Output and script are below.

[
  {
    "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"
  }
]

#!/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
1
Please follow the guidelines at minimal reproducible example as much as possible. What is the expected output? Also, if you are going to provide a script, please try to make it self-contained so that we can execute it.peak

1 Answers

9
votes

To add "siteurl":"http://mysite1.com" to every object:

jq 'map(.siteurl = "http://mytest1.com")' file.json

To copy siteurl from the first object:

jq '.[0].siteurl as $v | map(.siteurl = $v)' file.json