Hacker News new | ask | show | jobs
by l_t 2054 days ago
Not a theory per se, but my "lightbulb moment" with jq came when I thought about it like this:

jq is basically a templating language, like Jsonnet or Jinja2. What jq calls a "filter" can also be called a template for the output format.

Like any template, a jq filter will have the same structure as the desired output, but may also include dynamically calculated (interpolated) data, which can be a selection from the input data.

So, at a high level, write your filter to look like your output, with hardcoded data. Then, replace the "dynamic" parts of the output data with selectors over the input.

Don't worry about any of the other features (e.g. conditionals, variables) until you need them to write your "selectors."

YMMV, but that's what's worked for me