jq hack #1: colored less
Simon Shine

Simon Shine @sshine

About: I like purely functional programming and declarative domain-specific languages, especially query languages and infrastructure specification languages.

Location:
Copenhagen, Denmark
Joined:
Aug 19, 2019

jq hack #1: colored less

Publish Date: Sep 28 '20
6 2

Sometimes you want to look at a lot of JSON output in the terminal. Sometimes this JSON output is nicely formatted, and often it is compacted by a REST endpoint. Examples of verbose output:

$ kubectl get nodes -o json
$ curl 'https://hacker-news.firebaseio.com/v0/topstories.json'
Enter fullscreen mode Exit fullscreen mode

In both cases you can pipe the output through:

$ ... | jq -C . | less -R
Enter fullscreen mode Exit fullscreen mode

The -C parameter to jq forces colors. When omitted, jq will only print colors when an interactive terminal is detected. Piping further into less removes the colors. Additionally, we have to tell less to interpolate those ANSI colors so that we don't see a bunch of ESC[1;39m codes.

$ kubectl get nodes -o json | jq -C . | less -R
{
  "apiVersion": "v1",
  "items": [
    {
      "apiVersion": "v1",
      "kind": "Node",
      "metadata": {
        "annotations": {
...
:
Enter fullscreen mode Exit fullscreen mode

Here,

  • jq -C:

    --color-output / -C and --monochrome-output / -M:
    
    By default, jq outputs colored JSON if writing to a terminal.
    You can force it to produce color even if writing to a pipe
    or a file using -C, and disable color with -M.
    
  • less -R:

    -R or --RAW-CONTROL-CHARS
    
    Like -r, but only ANSI "color" escape sequences are output in
    "raw" form. Unlike -r, the screen appearance is maintained
    correctly in most cases. ANSI "color" escape sequences are
    sequences of the form:
    
        ESC [ ... m
    
    where the "..." is zero or more color specification characters.
    For the purpose of keeping track of screen appearance, ANSI color
    escape sequences are assumed to not move the cursor. [...]
    

Comments 2 total

Add comment