jq
is a very popular JSON processor.
Jackson JQ is an implementation of this processor in Java, and now you can
integrate it in your Quarkus application!
Simply add the dependency to your Quarkus project:
<dependency>
<groupId>io.quarkiverse.jackson-jq</groupId>
<artifactId>quarkus-jackson-jq</artifactId>
</dependency>
Then you can simply use the jackson-jq
's
Scope
bean in
your services. For example:
@Path("/jackson-jq")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class JacksonJqResource {
@Inject
Scope scope;
@POST
public List<JsonNode> parse(Document document) throws JsonQueryException {
final JsonQuery query = JsonQuery.compile(document.expression, Versions.JQ_1_6);
List<JsonNode> out = new ArrayList<>();
query.apply(this.scope, document.document, out::add);
return out;
}
}
You can also make use of the extra module by adding the following dependency:
<dependency>
<groupId>io.quarkiverse.jackson-jq</groupId>
<artifactId>quarkus-jackson-jq-extra</artifactId>
</dependency>
The extension support adding custom functions to the jq engine, as example, to add a function that accept one or two parameters:
import java.util.List;
import com.fasterxml.jackson.databind.JsonNode;
import io.quarkiverse.jackson.jq.JqFunction;
import net.thisptr.jackson.jq.Expression;
import net.thisptr.jackson.jq.Function;
import net.thisptr.jackson.jq.PathOutput;
import net.thisptr.jackson.jq.Scope;
import net.thisptr.jackson.jq.Version;
import net.thisptr.jackson.jq.exception.JsonQueryException;
import net.thisptr.jackson.jq.path.Path;
@JqFunction({ "myFunction/1", "myFunction/2" })
public class MyFunction implements Function {
@Override
public void apply(Scope scope, List<Expression> args, JsonNode in, Path path, PathOutput output, Version version)
throws JsonQueryException {
// add the content of your function here
}
}
The function will then be available to any jq expression i.e.
.items[] | myFunction("foo", "bar")
Underneath, this extension uses jackson-jq
, so the
same limitations and capabilities
of that library also applies here.
If you encounter any bugs or have any questions, please feel free to open an issue.
Thanks goes to these wonderful people (emoji key):
Ricardo Zanini 💻 🚧 |
Eiichi Sato 👀 |
Luca Burgazzoli 💻 |
Jiehong 💻 |
This project follows the all-contributors specification. Contributions of any kind welcome!