JPath -- search and validation utilities for JSON

JPath allows Java developers to easily inspect, query, and validate JSON objects. While it harnesses the power of Groovy's GPath and MOP to acheive this, you do not need to know or use Groovy to use JPath! JPath provides seamless integration into any JVM-based environment.

Why use JPath?

Consuming, querying, and validating JSON objects in Java is a tedious chore. Either you're stuck writing structured beans mapped to JSON responses, which is time-consuming and brittle, or you need to resort to circumlocutious chains of get() calls with typecasting (or foreknowledge) at every step. In particular, there is no equivalent (or even approximation) of XML's xquery, which allows for concise access to an object's information -- until now. Instead of this:

// assume json representation is in "jsonString"
JSONObject json = JSONObject(jsonString);
JSONObject foo = json.getJSONObject("foo");
JSONObject bar = foo.getJSONObject("bar");
JSONArray biz = bar.getJSONArray("biz");
String baz = null;
for (int i = 0; i < biz.size(); i++)
{
        baz = biz.get(i);
        if (baz != null && baz.equals("golly Moses")
        ...
}

// etc., etc.

And that's without null checks and other clutter. Wouldn't it be nicer to just say:

String s = jpath.followJpath(json, "foo.bar.biz.find{it == 'golly Moses'}");

With JPath you can. In addition, every response from JPath is a properly formatted, Java-safe JSON expression, or fragment thereof. But the big win is that JPath can perform runtime checks on JSON objects, including type checking and data constraints, allowing for concise 'schema' to accompany an JSON object. This in turn makes it simple to validate JSON without a complex toolchain.

For more in-depth instructions, see the syntax and usage page.