2 ms·
Instead of using recurse(), you can also define a function in jq and call that one recursively. E.g., the example from the OP could also be written as: def m
by xg15 5y ago
Instead of using recurse(), you can also define a function in jq and call that one recursively. E.g., the example from the OP could also be written as:
def my_func:
del(.children),
(.children[] | my_func)
;
[my_func]
(The function first emits its input object without the "children" field, then emits each element of the "children" array separately, after passing each through a recursive call of the function.
The ; char ends the function definition.
Finally, the function is called once on the top-level input and all emitted results are collected into an array.)
This is more verbose than recurse() but may sometimes be easier to understand if you want to know what is going on.