How to debug in `jq`

Some tips for using the `debug` function


jq comes with a handy debug filter. Use it while you are developing your exercise solutions to inspect the data that is currently in the jq pipline.

debug prints a "debug array" to stderr. It outputs the input unchanged. The first element of the array is the string "DEBUG:". The second element depends on how you invoke debug.

  1. the zero-arity debug function puts a compact representation of the input into the debug array:

    jq -n '[11, 22, 33] | debug | map(. * 2)'
    

    outputs

     ["DEBUG:",[11,22,33]]
     [
       22,
       44,
       66
     ]
    
  2. the one-arity version pipes the input through the given filter:

    jq -n '[11, 22, 33] | debug("length: \(length), last: \(.[-1])") | map(. * 2)'
    
     ["DEBUG:","length: 3, last: 33"]
     [
       22,
       44,
       66
     ]
    

    The filter doesn't need to be a string. It can be anything, including multiple comma-separated expressions for "multi-line" debug output:

    jq -n '[11, 22, 33] as $a | 44 | debug("I am here:", $a, .)'
    
    ["DEBUG:","I am here:"]
    ["DEBUG:",[11,22,33]]
    ["DEBUG:",44]
    44