AnsweredAssumed Answered

Have you heard of the jq command line tool for JSON parsing?

Question asked by Ryan Rutan on Aug 13, 2014
Latest reply on Sep 4, 2014 by jameswright

I just discovered this awesome tool today, and wanted to take a moment to share it with everyone   The tool is called jq.

 

To get it going, I used Homebrew for Mac, with a simple:

brew install jq

 

In my scenario I was trying to do some manipulating of securityGroup members, so I was using:

 

After some reading of the jq Manual, I was able to construct the following command:

curl -u $user:$pass -X GET -H "Content-Type: application/json" "https://your.jiveon.com/api/core/v3/securityGroups/$i/members" | grep -v 'allowIllegalResourceCall' | jq -r '.list[].resources.self.ref'

 

In essence, this command is:

  • connecting to my Jive Instance
  • getting a list of members for a security group
  • stripping the throw declaration
  • then parsing the JSON output
  • looking at the list property in the output and return all values resources.self.ref that exist in the items in the list array
    • the -r argument strips the quotes from the output (see below)

 

Raw V3 API Outputjq Output

throw 'allowIllegalResourceCall is false.';

{

  "itemsPerPage" : 25,

  "list" : [ {

    "id" : "1234",

    "resources" : {

      "trendingPlaces" : {

        "allowed" : [ "GET" ],

        "ref" : "https://your.jiveon.com/api/core/v3/people/1234/@trendingPlaces"

      },

      "trendingContent" : {

        "allowed" : [ "GET" ],

        "ref" : "https://your.jiveon.com/api/core/v3/people/1234/@trendingContent"

      },

....

"https://your.jiveon.com/api/core/v3/people/1234"

...

 

(note:  If you add the -r parameter to the command above) the result is:

https://your.jiveon.com/api/core/v3/people/1234

...

 

As you can see, this is a pretty simple way to traverse JSON and bring the power of the V3 API to shell scripting, especially some of these example: REST API v3 Examples

What I also love is that there is a free live parsing service for this tool called: jq play

This tool lets you put in some JSON and iterate on the proper jq syntax to get the output you want! ftw


Share Your Script on GitHub 

Lots of interesting tools ideas I have here, and going to share some of these scripts in this GitHub repo:

jivesoftware/scripts · GitHub

 

What do you think

Do you have some cool shell scripts that you use that you'd like to share?  Remember, contributing this repo constitutes points in the

From Points to Pints in the 2014-15 Jive Developer Program

 

Looking forward to the conversation, and hope this little find helps everyone =)

 

shorn jameswright brettvanderhaar it2000 - This seems right up your ally =)

Outcomes