We recently added the jq, a lightweight and flexible command-line JSON processor to the Oracle Linux yum server. jq is a very powerful JSON processor and it can come in handy when interacting with REST APIs Here's how you install it and a quick example of how to use it.

 

Steps

  1. Installing jq
  2. Using jq

 

1. Installing jq

The jq package is in the Addons repo on Oracle Linux yum server. Install yum-utils so that you can use yum-config-manager to enable the public_ol6_addons repo on Oracle Linux 6

 

$ sudo yum install yum-utils
$ sudo yum-config-manager --enable public_ol6_addons
$ sudo yum install jq

 

2. Using jq

Let's take a look at a public REST api on oracle.com that returns JSON:

 

$ curl -s "https://apicatalog.oraclecloud.com/v1/apicollections/?filter=name:bics&filter=version:1.0"

 

This returns:

 

{

  "data" : [ {

    "apis" : [ {

      "createdAt" : "2017-01-24T20:48:08.977Z",

      "description" : "Clear the cache so everyone sees the latest data.",

      "links" : {

        "canonical" : {

          "href" : "https://apicatalog.oraclecloud.com/v1/orgs/oracle-public/apicollections/bics/1.0/apis/delete-cached-data/canonical",

          "rel" : "canonical",

          "title" : "Canonical API Description",

          "type" : "application/swagger+json;version=2.0"

        },

        "parent" : {

          "href" : "https://apicatalog.oraclecloud.com/v1/orgs/oracle-public/apicollections/bics/1.0",

          "rel" : "parent",

          "title" : "Oracle BI Cloud Service",

          "type" : "application/json"

        },

        "raw" : {

          "href" : "https://apicatalog.oraclecloud.com/v1/orgs/oracle-public/apicollections/bics/1.0/apis/delete-cached-data/raw",

          "rel" : "raw",

          "title" : "Raw API Description",

          "type" : "application/swagger+json;version=2.0"

        },

        "self" : {

          "href" : "https://apicatalog.oraclecloud.com/v1/orgs/oracle-public/apicollections/bics/1.0/apis/delete-cached-data",

          "rel" : "self",

          "title" : "Delete Cached Data",

          "type" : "application/json"

        }

      },

      "name" : "delete-cached-data",

      "title" : "Delete Cached Data",

      "updatedAt" : "2017-02-04T03:10:53.877Z"

    }, {

      "createdAt" : "2017-01-24T20:48:08.977Z",

      "description" : "Get information about Oracle BI Cloud Service.",

      "links" : {

        "canonical" : {

          "href" : "https://apicatalog.oraclecloud.com/v1/orgs/oracle-public/apicollections/bics/1.0/apis/basic/canonical",

          "rel" : "canonical",

          "title" : "Canonical API Description",

          "type" : "application/swagger+json;version=2.0"

        },

        "parent" : {

          "href" : "https://apicatalog.oraclecloud.com/v1/orgs/oracle-public/apicollections/bics/1.0",

          "rel" : "parent",

          "title" : "Oracle BI Cloud Service",

          "type" : "application/json"

        },

        "raw" : {

          "href" : "https://apicatalog.oraclecloud.com/v1/orgs/oracle-public/apicollections/bics/1.0/apis/basic/raw",

          "rel" : "raw",

          "title" : "Raw API Description",

          "type" : "application/swagger+json;version=2.0"

        },

        "self" : {

          "href" : "https://apicatalog.oraclecloud.com/v1/orgs/oracle-public/apicollections/bics/1.0/apis/basic",

          "rel" : "self",

          "title" : "Get Basic Information",

          "type" : "application/json"

        }

      },

      "name" : "basic",

      "title" : "Get Basic Information",

      "updatedAt" : "2017-02-04T03:10:53.877Z"

    }, {

      "createdAt" : "2017-01-24T20:48:08.977Z",

      "description" : "Load and manage data sets for Oracle BI Cloud Service.",

      "links" : {

        "canonical" : {

          "href" : "https://apicatalog.oraclecloud.com/v1/orgs/oracle-public/apicollections/bics/1.0/apis/dataset-load/canonical",

          "rel" : "canonical",

          "title" : "Canonical API Description",

          "type" : "application/swagger+json;version=2.0"

        },

        "parent" : {

          "href" : "https://apicatalog.oraclecloud.com/v1/orgs/oracle-public/apicollections/bics/1.0",

          "rel" : "parent",

          "title" : "Oracle BI Cloud Service",

          "type" : "application/json"

        },

        "raw" : {

          "href" : "https://apicatalog.oraclecloud.com/v1/orgs/oracle-public/apicollections/bics/1.0/apis/dataset-load/raw",

          "rel" : "raw",

          "title" : "Raw API Description",

          "type" : "application/swagger+json;version=2.0"

        },

        "self" : {

          "href" : "https://apicatalog.oraclecloud.com/v1/orgs/oracle-public/apicollections/bics/1.0/apis/dataset-load",

          "rel" : "self",

          "title" : "Manage Data Sets",

          "type" : "application/json"

        }

      },

      "name" : "dataset-load",

      "title" : "Manage Data Sets",

      "updatedAt" : "2017-02-04T03:10:53.877Z"

    }, {

      "createdAt" : "2017-01-24T20:48:08.977Z",

      "description" : "Load and manage table data for Oracle BI Cloud Service.",

      "links" : {

        "canonical" : {

          "href" : "https://apicatalog.oraclecloud.com/v1/orgs/oracle-public/apicollections/bics/1.0/apis/table-load/canonical",

          "rel" : "canonical",

          "title" : "Canonical API Description",

          "type" : "application/swagger+json;version=2.0"

        },

        "parent" : {

          "href" : "https://apicatalog.oraclecloud.com/v1/orgs/oracle-public/apicollections/bics/1.0",

          "rel" : "parent",

          "title" : "Oracle BI Cloud Service",

          "type" : "application/json"

        },

        "raw" : {

          "href" : "https://apicatalog.oraclecloud.com/v1/orgs/oracle-public/apicollections/bics/1.0/apis/table-load/raw",

          "rel" : "raw",

          "title" : "Raw API Description",

          "type" : "application/swagger+json;version=2.0"

        },

        "self" : {

          "href" : "https://apicatalog.oraclecloud.com/v1/orgs/oracle-public/apicollections/bics/1.0/apis/table-load",

          "rel" : "self",

          "title" : "Manage Table Data",

          "type" : "application/json"

        }

      },

      "name" : "table-load",

      "title" : "Manage Table Data",

      "updatedAt" : "2017-02-04T03:10:53.877Z"

    }, {

      "createdAt" : "2017-01-24T20:48:08.977Z",

      "description" : "Create and manage relational tables and indexes that store data for Oracle BI Cloud Service.",

      "links" : {

        "canonical" : {

          "href" : "https://apicatalog.oraclecloud.com/v1/orgs/oracle-public/apicollections/bics/1.0/apis/tables-indexes/canonical",

          "rel" : "canonical",

          "title" : "Canonical API Description",

          "type" : "application/swagger+json;version=2.0"

        },

        "parent" : {

          "href" : "https://apicatalog.oraclecloud.com/v1/orgs/oracle-public/apicollections/bics/1.0",

          "rel" : "parent",

          "title" : "Oracle BI Cloud Service",

          "type" : "application/json"

        },

        "raw" : {

          "href" : "https://apicatalog.oraclecloud.com/v1/orgs/oracle-public/apicollections/bics/1.0/apis/tables-indexes/raw",

          "rel" : "raw",

          "title" : "Raw API Description",

          "type" : "application/swagger+json;version=2.0"

        },

        "self" : {

          "href" : "https://apicatalog.oraclecloud.com/v1/orgs/oracle-public/apicollections/bics/1.0/apis/tables-indexes",

          "rel" : "self",

          "title" : "Manage Tables and Indexes",

          "type" : "application/json"

        }

      },

      "name" : "tables-indexes",

      "title" : "Manage Tables and Indexes",

      "updatedAt" : "2017-02-04T03:10:53.877Z"

    } ],

    "category" : "/platform/business-analytics",

    "createdAt" : "2017-01-24T20:48:08.977Z",

    "defaultVersion" : true,

    "description" : "<p>Use the Oracle BI Cloud Service REST API to programmatically load and manage data.</p>",

    "links" : {

      "self" : {

        "href" : "https://apicatalog.oraclecloud.com/v1/orgs/oracle-public/apicollections/bics/1.0",

        "rel" : "self",

        "title" : "Oracle BI Cloud Service",

        "type" : "application/json"

      }

    },

    "name" : "bics",

    "organization" : "oracle-public",

    "tags" : [ ],

    "title" : "Oracle BI Cloud Service",

    "updatedAt" : "2017-02-04T03:10:53.877Z",

    "version" : "1.0"

  } ],

  "links" : {

    "self" : {

      "href" : "https://apicatalog.oraclecloud.com/v1/apicollections/?filter=name%3Abics&filter=version%3A1.0",

      "rel" : "self",

      "title" : "Page 1",

      "type" : "application/json"

    }

  },

  "pageNumber" : 1,

  "pageSize" : 10,

  "totalCount" : 1

 

To process this JSON to display only the API descriptions, pipe the output of curl to jq with a filter that iterates over the data array and the apis array within it and selects only the values for the description key.

 

curl -s "https://apicatalog.oraclecloud.com/v1/apicollections/?filter=name:bics&filter=version:1.0" | jq '.data[] .apis[] .description'

 

This returns:

 

"Clear the cache so everyone sees the latest data."

"Get information about Oracle BI Cloud Service."

"Load and manage data sets for Oracle BI Cloud Service."

"Load and manage table data for Oracle BI Cloud Service."

"Create and manage relational tables and indexes that store data for Oracle BI Cloud Service."

For more advanced examples of processing JSON with jq, see References below.

 

References