Difficulty: beginner
Estimated Time: 25 minutes

jq is a Linux command line utility that is easily used to extract data from JSON documents. Source of JSON document can be responses from CLI commands or REST API calls, files retrieved from remote locations or read from local storage.

jq is like awk, grep or sed for JSON data - you can use it to slice and filter and map and transform structured data with the same ease that sed, awk, grep and friends let you play with text.

jq is written in portable C, and it has zero runtime dependencies. You can download a single binary, scp it to a far away machine of the same type, and expect it to work.

In this scenario, you will a few simple steps to take using jq, just to get you going.

Some resources

Summary

This completes the scenario for managing the OCI Lab Environment for the Katacoda OCI Scenarios.

Introduction to JQ - Linux Command Line tool for JSON

Step 1 of 3

Step 1 -

First steps - really simple

Here we first look at some of the examples I have gathered from other resources:

Let’s start by taking a look at the simplest filter of all which incidentally is one of the most useful and frequently used features of jq:

echo '{"fruit":{"name":"apple","color":"green","price":1.20}}' | jq '.' In this example, we echo a simple JSON string and pipe it directly into our jq command. Then, we use the identity filter ‘.’ which takes the input and produces it unchanged as output with the caveat that by default jq pretty-prints all output.

Slightly more interesting would be to use an expression to extract a particular property - the color of the fruit object - from the JSON document:

echo '{"fruit":{"name":"apple","color":"green","price":1.20}}' | jq '.fruit | .color'

jq can work with arrays just as easily as with plain or nested objects.

basket='[ { "name": "apple", "color": "green", "price": 1.2 }, { "name": "banana", "color": "yellow", "price": 0.5 }, { "name": "kiwi", "color": "green", "price": 1.25 } ]'

List the names of the fruits in the basket:

echo $basket | jq '.[].name '

Additional functions in jq can be applied to arrays, such as unique : echo $basket | jq 'map(.color) | unique'