Whenever I'm working with JSON content on the Linux command line (which usually involves some scripting or monitoring checks), one of the first things is to analyze the JSON output. Understanding the keys and their nested content (if there is) is the first part. Here's how to retrieve these keys - and their nested keys.
In the past I usually used jshon to handle JSON content, but in recent years I have switched more and more to the jq alternative. Not only because it's newer, mainly because it exists on both Debian derivatives and Enterprise Linux distributions. And good news: jq is well maintained (once again) since 2023!
While I still think jshon is easier to use and more "intuitive", jq definitely has much more powerful features. Especially the select() function is something I love! But let's compare the two of them.
The following examples use the Salesforce status API, which can be used to monitor a Salesforce instance.
To show the keys using jshon, the parameter -k is used. That's easy to remember and prints the keys found at the top level:
ck@mint ~ $ curl -s "https://api.status.salesforce.com/v1/instances/CS110/status" | jshon -k
key
location
environment
releaseVersion
releaseNumber
status
isActive
city
stateName
stateCode
countryName
countryCode
maintenanceWindow
Services
Products
Incidents
Maintenances
Tags
GeneralMessage
To find nested keys, let's say inside "Services", this key needs to be selected using the -e (extract) parameter:
ck@mint ~ $ curl -s "https://api.status.salesforce.com/v1/instances/CS110/status" | jshon -e Services -k
parse error: type 'array' has no keys (arg 3)
The error here mentions that under Services a nested array was found. For this situation the -a (for array) needs to be appended after selecting the Services key:
ck@mint ~ $ curl -s "https://api.status.salesforce.com/v1/instances/CS110/status" | jshon -e Services -a -k
key
order
isCore
key
order
isCore
key
order
isCore
key
order
isCore
key
order
isCore
key
order
isCore
key
order
isCore
key
order
isCore
key
order
isCore
key
order
isCore
key
order
isCore
key
order
isCore
key
order
isCore
key
order
isCore
key
order
isCore
To print the values of the "key" (which represents the Salesforce Service Name) inside the nested array under "Services", once again extract this key named "key" (stupid example, I know):
ck@mint ~ $ curl -s "https://api.status.salesforce.com/v1/instances/CS110/status" | jshon -e Services -a -e key
"coreService"
"liveAgent"
"EinsteinBots"
"Communities"
"search"
"Customer360Audiences"
"analytics"
"CPQandBilling"
"ServiceCloudVoice"
"SalesforceCMS"
"SALESFORCEORDERMANAGEMENT"
"B2BCommerce"
"B2B2CCommerce"
"SalesforceUnifiedMessaging"
"ServiceCloudMessagingChannels"
However with jshon I was not able to find an example (or any documentation) how to print only the first value (array index 0).
Now let's use jq to do the same. Let's first show the keys once again. jq doesn't have command line parameters as jshon, everything (including functions) is inside the 'jq string'.
ck@mint ~ $ curl -s "https://api.status.salesforce.com/v1/instances/CS110/status" | jq 'keys'
[
"GeneralMessage",
"Incidents",
"Maintenances",
"Products",
"Services",
"Tags",
"city",
"countryCode",
"countryName",
"environment",
"isActive",
"key",
"location",
"maintenanceWindow",
"releaseNumber",
"releaseVersion",
"stateCode",
"stateName",
"status"
]
Compared to jshon, the returned keys are sorted by alphabet (capital letters first). To ignore a sorted list, use 'keys_unsorted':
ck@mint ~ $ curl -s "https://api.status.salesforce.com/v1/instances/CS110/status" | jq 'keys_unsorted'
[
"key",
"location",
"environment",
"releaseVersion",
"releaseNumber",
"status",
"isActive",
"city",
"stateName",
"stateCode",
"countryName",
"countryCode",
"maintenanceWindow",
"Services",
"Products",
"Incidents",
"Maintenances",
"Tags",
"GeneralMessage"
]
This is now the same output as above with jshon -k.
Now let's go deeper and look at the keys of the nested "Services". As we know from above, there is a nested array under "Services", so we need to tell this to jq:
ck@mint ~ $ curl -s "https://api.status.salesforce.com/v1/instances/CS110/status" | jq '.Services[]|keys_unsorted'
[
"key",
"order",
"isCore"
]
[
"key",
"order",
"isCore"
]
[
"key",
"order",
"isCore"
]
[
"key",
"order",
"isCore"
]
[
"key",
"order",
"isCore"
]
[
"key",
"order",
"isCore"
]
[
"key",
"order",
"isCore"
]
[
"key",
"order",
"isCore"
]
[
"key",
"order",
"isCore"
]
[
"key",
"order",
"isCore"
]
[
"key",
"order",
"isCore"
]
[
"key",
"order",
"isCore"
]
[
"key",
"order",
"isCore"
]
[
"key",
"order",
"isCore"
]
[
"key",
"order",
"isCore"
]
To only get the keys of the first element in the array (array[0]), we can use:
ck@mint ~ $ curl -s "https://api.status.salesforce.com/v1/instances/CS110/status" | jq '.Services[0]|keys_unsorted'
[
"key",
"order",
"isCore"
]
This helps to understand the structure of the nested array and their elements - as long as they are the same across all entries.
To show the Salesforce service names (reminder: the value is under the key "key"):
ck@mint ~ $ curl -s "https://api.status.salesforce.com/v1/instances/CS110/status" | jq '.Services[].key'
"coreService"
"liveAgent"
"EinsteinBots"
"Communities"
"search"
"Customer360Audiences"
"analytics"
"CPQandBilling"
"ServiceCloudVoice"
"SalesforceCMS"
"SALESFORCEORDERMANAGEMENT"
"B2BCommerce"
"B2B2CCommerce"
"SalesforceUnifiedMessaging"
"ServiceCloudMessagingChannels"
And to only get the service name from the first array entry:
ck@mint ~ $ curl -s "https://api.status.salesforce.com/v1/instances/CS110/status" | jq '.Services[0].key'
"coreService"
At the beginning I mentioned I love the select() function of jq. Here's an example using this.
Let's say we want to find out the value of the "isCore" key, but only if the service name ("key") is "coreService":
ck@mint ~ $ curl -s "https://api.status.salesforce.com/v1/instances/CS110/status" | jq '.Services[] | select(.key == "coreService").isCore'
true
To explain this: jq is told to parse the nested array under "Services" and search (select) for an entry where the "key" value matches the string "coreService". For this found entry print the value of "isCore".
No comments yet.
AWS Android Ansible Apache Apple Atlassian BSD Backup Bash Bluecoat CMS Chef Cloud Coding Consul Containers CouchDB DB DNS Database Databases Docker ELK Elasticsearch Filebeat FreeBSD Galera Git GlusterFS Grafana Graphics HAProxy HTML Hacks Hardware Icinga Influx Internet Java KVM Kibana Kodi Kubernetes LVM LXC Linux Logstash Mac Macintosh Mail MariaDB Minio MongoDB Monitoring Multimedia MySQL NFS Nagios Network Nginx OSSEC OTRS Office OpenSearch PGSQL PHP Perl Personal PostgreSQL Postgres PowerDNS Proxmox Proxy Python Rancher Rant Redis Roundcube SSL Samba Seafile Security Shell SmartOS Solaris Surveillance Systemd TLS Tomcat Ubuntu Unix VMWare VMware Varnish Virtualization Windows Wireless Wordpress Wyse ZFS Zoneminder