Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easier way to find where in jq code an error occurred?

Tags:

debugging

jq

Is there a way for jq to give a reference to where in code an error occurred?

Too often I end up with errors that are unhelpful. Here is an example:

jq: error (at <stdin>:43913): Cannot index object with null

With or without the --slurp flag, the line indicator for stdin is almost always the last line of input. What would be really helpful is to note where in the code it failed at runtime.

Wrapping code snippets in try/catch blocks with $__loc__ have proven unhelpful too, as the line in the code tends to be the catch block, not where the error occurred.

jq: error (at <stdin>:43913) (not a string): {"file":"<top-level>","line":68}

Is there some method to make debugging jq scripts easier?

What I've been doing instead is commenting out large chunks of code and performing a binary search for what code gets the error. It feels like there is a better way.

like image 710
ericslaw Avatar asked Nov 28 '25 04:11

ericslaw


1 Answers

Sometimes, using gojq ("the Go implementation of jq") gives good insights about errors.

Otherwise, you will probably find that some combination of debug statements, $__loc__, and debugging functions defined using debug to be most helpful.

Consider, for example:

def debug(msg): (msg|debug) as $debug | . ;

(The subtlety here is that you can use "\(.)" in msg.)

In a pinch, the command-line option --debug-trace can be useful.

like image 187
peak Avatar answered Nov 29 '25 22:11

peak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!