Search code examples
jqtee

filter that prints a value and then forwards the entire json document to the next filter, like tee


Is it possible to print parts of the document in a filter and then move on to select further down and print more later?

Here is pseudo code for what I want.

{
    "version":"1",
    "some":{
        "more":{
            "depth":"here"
        }
    }
}

select(.some.more.depth=="here") | tee .version | .some.more.depth

This would output

"1"
"here"

I know, that in this case, it would work with .version, .some.more.depth but in a more complex case it's more about working down the document while printing parts along the way.

https://jqplay.org/s/n7WjphEVf7


Solution

  • Not just in this case, in any case. That's what the comma operator does, and what it's for. It runs two expressions in the same context and produces all of the outputs of both. Remember that you can always use parentheses, so it's legit to do things like

    .a.b.c | (.d, .e.f | (.g, .h))
    

    to produce .a.b.c.d, .a.b.c.e.f.g, and .a.b.c.e.f.h