0

I need to take some JSON, slice it using JQ so New Relic can parse it properly as it only takes a specific format when pushing JSON to their dashboards.

I am using a bash script and WPCLI and then formatting the JSONs output using JQ.

Below is my script.

#!/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 .[])"
                PLUGINS="$(wp plugin list --format=json | jq .[])"

                echo "${SITEURL}" "${USERS}" "${PLUGINS}"
                cd ../.. ;
    fi
done

Below is the output I am getting...

cat wp-info-output 
http://mytest1.com {
  "display_name": "testuser1",
  "user_email": "test.user@testing.com",
  "user_registered": "2018-11-26 17:44:09",
  "roles": "administrator"
} {
  "name": "akismet",
  "status": "inactive",
  "update": "available",
  "version": "4.0.8"
}
{
  "name": "hello",
  "status": "inactive",
  "update": "none",
  "version": "1.7"
}

I need it to look like this...

[
  {
    "eventType":"WordpressSite",
    "siteurl":"http://mytest1.com"
  },
  {
    "eventType":"WordpressPlugins",
    "pluginName": "akismet",
    "status": "inactive",
    "update": "available",
    "version": "4.0.8"
  },
    {
    "eventType":"WordpressPlugins",
    "pluginName": "hello",
    "status": "inactive",
    "update": "none",
    "version": "1.7"
  }
  {
    "eventType":"Users",
    "display_name": "testuser2",
    "user_email": "test.user@testing.com",
    "user_registered": "2018-11-26 17:44:04",
    "roles": "administrator"
  }
]

I think jq is my answer for manipulating the JSON to be formatted but I am hitting some walls.

  • Note that your directory variable $f holds an absolute path, so there's really no need to cd ../.. in the loop – glenn jackman Nov 30 '18 at 20:44
1

It seems you want to take all those JSON objects and smush them together in one bit array: try

siteurl=$(wp option get siteurl)
users=$(
    wp user list --fields=display_name,user_email,user_registered,roles --format=json) |
    jq '.[] | .eventType = "WordpressUser"'
)
plugins=$( wp plugin list --format=json | jq '.[] | .eventType = "WordpressPlugin"' )

{
    printf '{"eventType":"WordpressSite","siteurl":"%s"}\n' "$(wp option get siteurl)"
    echo "$plugins"
    echo "$users"
} | jq -s .
  • Yea I know it seems goofy but that's how New Relic want's their json events formatted. Each JSON event should be it's own object, without any real arrays. Therefore, each event would need to be its own object with a header event type. You can see more here: docs.newrelic.com/docs/insights/insights-data-sources/… But I pasted the example New Relic wants. – systemfive Nov 30 '18 at 21:27
  • Also, what you provided got me 99% there! The only thing is the eventType for the plugins is at the bottom of the JSON object instead of at the top. – systemfive Nov 30 '18 at 21:41
  • The order should not matter. The attributes of a JSON object are not inherently ordered. – glenn jackman Nov 30 '18 at 21:42
  • Ah you're right, it really shouldn't matter. For some reason the Users are not being outputted but I think you got me 99% there and I can continue to hack it out. Thanks! – systemfive Nov 30 '18 at 21:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.