X-47 Industries

Exploring tools and infrastructure for systems administration.

Json, JQ and Gron

Json has become a lingua franca. The modern world of api-driven, -as-a-Service platforms put json front and center, using it as a default structure for passing around large data objects. If, like me, you live in unix-land full-time, you may have been able to ignore it. The AWS cli, in particular, gives options for ‘text’ and ‘table’ in the ‘–option’ flag; and these go a long way toward delaying the need to read json directly, but the day will come and what’s a unix-speaker to do?

JQ is the common tool for this. It follows the unix precept of small tool, doing one thing well, that is to be chained with other tools. It has its own language for specifying what part of the json structure to parse, and that language itself echoes unix’s step-wise, composable nature. For example, the command jq -r ‘.[] | .name’ listing.json; here .name is simply the attribute we want to read from whatever’s stored in listing.json. While I do think it’s a pretty easy tool to get started with, there are better sources to walk you through the in’s and outs of jq: JQ is sed for json; Bash that json.

The challenge I find in working with large, highly nested json objects is that it’s very easy to get lost. When the nesting is five or six levels deep, and when screens of data have paged by, all the pretty-printing in the world doesn’t help me keep track of where I’m at. In those instances, I reach for Gron. While it’s stated purpose is to make json greppable (and it does a fine job of that), I appreciate it more because it describes the structure of the json. For example:

$ gron flintstones.json
json = [];
json[0] = {};
json[0].name = “fred”;
json[0].surname = “flintsone”;
json[1] = {};
json[1].name = “wilma”;
json[1].surname = “flintstone”;
json[2] = {};
json[2].name = “barney”;
json[2].surname = “rubble”;
json[3] = {};
json[3].name = “betty”;
json[3].surname = “rubble”;

Here, we have a json array with datapoints, arrays of names and surnames. In a trivial example like this, it’s not hard to write to write the jq filter ‘.[].name’ to list just the first names. However, with gron, I can look at any of the characters, and see in an instant, how the structure is nested. Writing the needed jq is almost cut-and-paste.

Hopefully, you are beginning to see how json can work alongside everything else in the world of unix. JQ is a powerful, accessible tool for manipulating and parsing json. Gron, too, is helpful; especially if you’re getting lost in highly nested json. These tools quickly help you wrangle the gnarliest of json, bringing it into the realm of your tried and true unix toolset.