Java-Web:Using jq to process Json - Exercises

From Juneday education
Jump to: navigation, search

We will soon add exercises on jq here. In the meanwhile, please copy all steps on the previous page and verify that you can follow them in your own terminal.

Exercises work in progress

Get Json from Punk API

Here's an API we'll use for these exercises:

Read the documentation carefully, and then find the query to use in order to get a random beer (it's documented on the paged linked above).

Expand using link to the right to see the suggested solution.

Now, use lwp-request -m GET or curl -s to fetch a random beer so that it is printed to standard out.

Expand using link to the right to see the suggested solution.

lwp-request -m GET
curl -s

Get a picture of the structure of all that Json

That's quite a lot of data, isn't it?

Let's pipe it through jq '.' to at least make it more readable. Do it!

Expand using link to the right to see the suggested solution.

This is what the random beer we got looked like:

$ curl -s | jq '.'
    "id": 193,
    "name": "Blitz Berliner Weisse",
    "tagline": "Berliner Fruit Beer.",
    "first_brewed": "07/2013",
    "description": "Our sour recipe for all fruit Blitz beers uses a process called kettle souring. In this we steep a bag of malt in the wort to allow the bacteria to grow in it.",
    "image_url": "",
    "abv": 3,
    "ibu": 8,
    "target_fg": 1040,
    "target_og": 1007,
    "ebc": 9,
    "srm": 4.5,
    "ph": 3.2,
    "attenuation_level": 82.5,
    "volume": {
      "value": 20,
      "unit": "liters"
    "boil_volume": {
      "value": 25,
      "unit": "liters"
    "method": {
      "mash_temp": [
          "temp": {
            "value": 65,
            "unit": "celsius"
          "duration": 60
      "fermentation": {
        "temp": {
          "value": 19,
          "unit": "celsius"
      "twist": "Before main fermentation steep a bag of malt in the wort for 48 hours. Re boil the wort before pitching in the yeast for the main fermentation."
    "ingredients": {
      "malt": [
          "name": "Extra Pale",
          "amount": {
            "value": 1.88,
            "unit": "kilograms"
          "name": "Wheat",
          "amount": {
            "value": 1.88,
            "unit": "kilograms"
      "hops": [
          "name": "Magnum",
          "amount": {
            "value": 8.5,
            "unit": "grams"
          "add": "start",
          "attribute": "bitter"
      "yeast": "Wyeast 1056 - American Ale™"
    "food_pairing": [
      "Grilled salmon with a light lemon sauce",
      "Lobster bisque",
      "Cheesecake with raspberry (or peach/passion fruit) sauce"
    "brewers_tips": "Making sour beers is an art, and is mostly related to trial and error with your process. Experiment with different temperatures for steeping the grain. 40 - 50°C willallow the lacto to grow and produce a clean profile.",
    "contributed_by": "Ali Skinner <AliSkinner>"

With such amount of data, it is hard to know how to parse it with jq.

Let's make jq give us a hint of the structure. We made a script for that!

The script tries to create paths to all elements in a Json, so that you get an overview of the Json structure.

We think the script is good enough™.

$ curl -s | ./


Let's try it:

$ curl -s | jq '.[0]."ingredients"."hops"[0]."name"'
$ curl -s | jq '.[0]."method"."mash_temp"[0]."duration"'

.[0]."ingredients"."hops" is an array, we can see. The full path to use to get all hops' names as an array will then be:

jq '[{hop_name:.[0]."ingredients"."hops"[]."name"}]'
$ curl -s | jq '[{"hop_name":.[0]."ingredients"."hops"[]."name"}]'
  "hop_name": "Magnum"
  "hop_name": "Willamette"
  "hop_name": "First Gold"

Now it's your turn

Use the outline from our script, to list all food pairings for some random beer. We want you to include the name of the beer, and an array called foods.

Expand using link to the right to see the suggested solution.

$ curl -s | jq '{name: .[0]."name", foods:.[0]."food_pairing"}'
  "name": "10 Heads High",
  "foods": [
    "Arrabiata pasta",
    "Mulled cheddar",
    "Toasted almond cake"


Source code

Where to go next

The previous page was Java-Web:Using_jq_to_process_Json with the theory for these exercises.

The Next page is a new topic, Java-Web:Creating_JSON_from_Java

The TOC goes to Java_Web_programming.

« PreviousBook TOCNext »