Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Tutorial

gq programs can be thought of as a series of transformations on JSON with special support for GeoJSON.

Types

All JSON types are supported: numbers, strings, booleans, arrays, objects, and null. You construct and nest them in a familiar way:

[{"a": 1}, true, false, null, 2.5, "hello"]

It is important to note that in gq both the input and output must be valid JSON (this is different from jq which only requires that the input be JSON). Moreover, the data must always be valid JSON at every step in the process of executing a gq program.

Identity

The simplest gq program is just a dot: .. This is the identity function which simply passes the input along and does nothing with it.

For example:

echo '[1, 2, 3]' | gq '.'

returns

[
  1,
  2,
  3
]

Try in the gq Playground

Notice that, like in jq, the JSON output is pretty-printed by default.

Property and Index Lookups

You can look up object properties via .<property_name>:

echo '{"a": 1}' | gq '.a'

returns

1

Array index lookups work similarly (zero-based):

echo '[1, 2, 3]' | gq '.[1]'

returns

2

These lookups can be chained together:

echo '{"a": {"b": {"c": [1, 2, 3]}}}' | gq '.a.b.c[0]'

returns

1

Try in the gq Playground

This nested lookup can also be expressed as:

.a | .b | .c | .[0]

If a non-existent key or index is requested:

echo '{"a": 1}' | gq '.z'

null is returned.

Pipe

The | operator chains sub-expressions together.

In this example, we look up the value of property a and pass that value off to another expression that adds 1 to it:

echo '{"a": 1}' | gq '.a | . + 1'

returns

2

The .a expression looks up the value of the property “a” and returns it.

The | passes that return value into the next expression. In the context of that expression, the value of . is that return value of 1. We then add 1 to 1 and get 2.

If we go ahead and compare that result to 2:

echo '{"a": 1}' | gq '.a | . + 1 == 2'

returns

true

Try in the gq Playground

As you can see, typical arithmetic (+, -, *, /, %) and comparison (==, !=, >, <, >=, <=) operators are supported in ways that should hopefully be familiar.

Assignment

The = operator assigns values and works with lookups:

echo '{"a": 1}' | gq '.a = 2'

returns

{
  "a": 2
}

It also works with nested lookups:

echo '{"a": {"b": 1}}' | gq '.a.b = 2'

returns

{
  "a": {
    "b": 2
  }
}

Note that if the key does not exist, it will be created automatically, including any intermediate structures, with null filled in for any missing values.

For example:

echo '{"a": 1}' | gq '.a.b[1].c = 2'

returns

{
  "a": {
    "b": [
      null,
      {
        "c": 2
      }
    ]
  }
}

Try in the gq Playground

There is also the update assignment operator (|=), which is subtly different and may be more appropriate depending on your use case (see this section of the jq manual), as well as arithmetic update operators (+=, -=, *=, /=, %=).

Looping and filtering

There are no loops in gq, but the built-in functions map and filter should meet a similar need.

As with other programming languages, map applies a function to every item in a collection:

echo '[1, 2, 3]' | gq 'map(. * 2)'

returns

[
  2,
  4,
  6
]

Try in the gq Playground

And filter keeps only items in a collection for which the applied function returns true:

echo '[1, 2, 3]' | gq 'filter(. > 1)'

returns

[
  2,
  3
]

Try in the gq Playground

Aggregation

There is deliberately no reduce function in the interest of simplicity.

Hopefully, built-in functions (eg. add, length) will meet your needs in cases where you might otherwise want to use reduce. If not, please see Issues.

Accessing original input data

We’ve looked at . as a way to reference the current state of the program. There are also special variables automatically available that reference the original value passed into the program.

The variable $0 references the original input:

echo '[1, 2, 3]' | gq 'add | {"original": $0, "sum": .}'

returns

{
  "original": [
    1,
    2,
    3
  ],
  "sum": 6
}

Try in the gq Playground

Working with GeoJSON

gq’s main purpose is to manipulate GeoJSON, and all functionality is ultimately in service of this goal.

Let’s look at a practical example given a FeatureCollection like so, saved locally as feature_collection.json:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [0, 0],
            [2, 0],
            [2, 2],
            [0, 2],
            [0, 0]
          ]
        ]
      },
      "properties": null
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [1, 1],
            [3, 1],
            [3, 3],
            [1, 3],
            [1, 1]
          ]
        ]
      },
      "properties": null
    }
  ]
}

Union

We can merge the geometries of all of its Features via:

gq 'union' feature_collection.json

returning a new GeoJSON Geometry:

{
  "type": "Polygon",
  "coordinates": [
    [
      [0, 2],
      [0, 0],
      [2, 0],
      [2, 1],
      [3, 1],
      [3, 3],
      [1, 3],
      [1, 2],
      [0, 2]
    ]
  ]
}

Try in the gq Playground

Feature construction

We can then wrap that in a Feature via:

gq 'union | feature' feature_collection.json

returning:

{
  "type": "Feature",
  "geometry": {
    "type": "Polygon",
    "coordinates": [
      [
        [0, 2],
        [0, 0],
        [2, 0],
        [2, 1],
        [3, 1],
        [3, 3],
        [1, 3],
        [1, 2],
        [0, 2]
      ]
    ]
  },
  "properties": null
}

Try in the gq Playground

Bounding box

We can set the bounding box on the feature via:

gq 'union | feature | .bounding_box = (.geometry | bounding_box)' feature_collection.json

This sets the bounding_box property on the feature to output of feeding the Feature’s geometry into the bounding_box function.

The output then looks like this:

{
  "type": "Feature",
  "geometry": {
    "type": "Polygon",
    "coordinates": [
      [
        [0, 2],
        [0, 0],
        [2, 0],
        [2, 1],
        [3, 1],
        [3, 3],
        [1, 3],
        [1, 2],
        [0, 2]
      ]
    ]
  },
  "properties": null,
  "bounding_box": {
    "type": "Polygon",
    "coordinates": [
      [
        [3, 0],
        [3, 3],
        [0, 3],
        [0, 0],
        [3, 0]
      ]
    ]
  }
}

Try in the gq Playground

Validation

Finally, we can validate our output as valid GeoJSON via validate, which only returns an error if the GeoJSON is invalid and otherwise does nothing. In this case, the GeoJSON is indeed valid!

The complete gq command is:

gq 'union | feature | .bounding_box = (.geometry | bounding_box) | validate' feature_collection.json

Try in the gq Playground