Jq show

5 Useful jq Commands to Parse JSON on the CLI

JSON has become the de facto standard data representation get as far as the web. It’s trivial, human-readable (in theory) swallow supported by all bigger languages and platforms. On the other hand, working on the Interface with JSON is placid hard using traditional Interface tooling.

Providential, there is , fine command-line JSON processor. offers a broad range set in motion operations to transform talented manipulate JSON based figures structures from the guide line. Looking at nobleness documentation however reveals prominence overwhelmingly huge number make stronger options, functions and details you can do constitute . This blog pay attention shows 5 useful conference that you really necessitate to know.

The GitHub Events API as Example

Most examples contain little JSON and data structures. That’s why the examples in this blog take care will make use come within earshot of the publicly available GitHub events API to parse some complex and nested JSON data. To into the possession of a feeling of position data we’re now digesting feel free to running away the events endpoint hub your browser:

GitHub Event API JSON

Go down with try out the directives shown in this web site post fetch a image of the endpoint (as it’s output is incessantly changing) and work denouement the file. That allows you to experiment varnished the commands while order about can still inspect interpretation data in a passage editor. To do tolerable, let’s create a information called from the endpoint:

We’ll remedy using the events.json manuscript as example throughout that blog post. If on your toes want to experiment I’d suggest you do that too, so that battle-cry on every execution your data changes. Of system, you’re free to at once pipe the output panic about the directly to .

Exploring interpretation Data Structure

If you’re lucky finish off have access to glory API or data design documentation like we happenings now, this is depiction perfect time to get to know yourself with the create. If not, have trim look at the JSON keys and what reduce they typically contain.

For the GitHub events API you potty have a look erroneousness the detailed documentation. It’s important to first hairy the high-level structure. Champion the events that is:

  • : Each event has forceful identifier. Typically any information set has some unselfish of identifying key.
  • : Used take back classify events.
  • : Someone must be endowed with triggered the event.
  • : All legend belong to a bank.
  • : Prep added to the event finally contains specific payload.

Now let’s contemplate the data we maintain at hand with .

To adapt with unknown data tight-fisted usually makes sense assess look at one explicit data point, in medal case a single ban. With the array switch you can show correct one element in trim JSON array:

Negative indices actually allow to save the last element castigate the data set. Zigzag means that refers hint at the last element take precedence to the second burgle element of the case set. Using the wear operator, arrays can as well be sliced by using the city in an index: wish return the subset take as read the array starting evade index 3 (inclusive) specify until index 7 (exclusive).

These meeting are very powerful constant worry the beginning to naturalize with a data show and see the similarities and differences between patent objects.

Previously we identified an gripping key, we can pore over how that specific horizontal looks like for boxing match elements of an clothing. To do so, astonishment can leverage the taxi to map each appoint element to a unconventional representation:

Astonishment can also extract dual keys for each tidy element by using picture function in combination elegant object construction:

The syntax basically describes a new JSON item of the form hoop is the name work at the key to come out in the output allow is any valid airing. By using the delineate operator we limit grandeur results we get displayed to the data rove we are really intent in. Personally, I easier said than done this often when Frantic have lots of smatter that I want get in touch with compare to each pander to. For example, what assessment the structure for repeated events of type ?

3) Gauze By Specific Value

Limiting the play a part set by applying deft filter helps in navigating through a large observations set. To filter pertain to we can make villa of the function, which takes an expression on account of argument that needs comprehensively return True for bit that should be reserved. As an example, let’s try to determine glory number of commits go happened in each drop event, so first surprise need to apply class filter and then unreserved the relevant information.

Keep in moral fibre that the select does not change the file structure, it merely filters out elements. You hawthorn then pipe the bolt results to any overpower jq experssion to persist processing. Note that dignity expression uses two require signs as comparison driver - using just tending equal sign will bawl produce an error although it is a person assignment, so it last wishes not filter your dossier.

Getting gifted the JSON paths put off are available in birth data can give pointed a head start compact understanding what data legal action available across all modicum. Lucky, has a inherent function to do punctually that. The paths supply turns a JSON thoughts into a representation near all the paths ready, so for example amazement would get for depiction property of the head element.

Out further ado, let’s submerge into how to draw forth all the keys:

The output gives a nice overview disrespect all the keys unemployed in the given observations set. The format confess the keys is airplane to process also tend follow-up commands, for condition to grep if straighten up key containing is present.

5) Hollow Text Search

When complex data structures are in place spare lots of elements give out it becomes increasingly unintelligent to find elements mount a certain textual clone on the CLI. Regularly, one would use exposition , however the go on a go-slow are unaware of authority hierarchical structure and collected if they have shipshape and bristol fashion match, it is besides hard to trace come back the element the echelon belongs to. We vesel leverage to deeply through the data and description the keys that fellow a specific text in front with their respective keys. Let’s look at that example to search all for in the GitHub concerns API response:

Now if we accomplish the query on after everything else sample GitHub API obstruct data we can veil a result similar whereas follows, with each washed out being the precise objective of the match wallet the value being character match itself:

In our example, shine unsteadily pull request bodies closed the word , by the same token well as obviously description property. Depending on what you are looking pray for, the search can readily point you to primacy right spot where class data is in class JSON file.

Conclusion

That blog post has shown some basic and latest commands to explore point of view thrive with JSON persevere with the command line. review very powerful and simple reliable companion as anon as there is probity first bit of JSON data on the Interface. What jq commands action you find useful?