JQ def / helper functions example

Define reusable helper functions in jq with def. Interactive example showing how to declare a function and apply it inside a pipeline.

The jq expression

def isLocalhost: . == "127.0.0.1";

.hosts[] | {localhost: .host | isLocalhost}

def name: body; declares a reusable jq function that you can call later in the pipeline, which keeps complex filters readable. Here we define isLocalhost to test whether a value equals "127.0.0.1", then apply it to each host to flag which ones are localhost.