What is JSONPath
JSONPath is a tool based on XPath that allows us to do with JSON what XPath allows us to do with XML. You might be asking: “What does XPath allow us to do for XML that we want to be able to do the same with JSON?” XPath (and by extension, JSONPath), is a small utility that gives us an easy to use/ understand syntax for picking certain parts of a given XML (or JSON) data. We can liken it to RegEx although their purpose and results are very different. Both take a data and a ruleset and give us data that we’re looking for. We can think of the ruleset we’re giving as a very simple scripting language (but not a DSL, don’t be scared). It is more like linux’
grep utility than a full-blown scripting language like python. JSONPath does this job not for XML, but for it’s much popular little cousin, JSON, who is overtaking XML on most of the web.
The problems it can solve are said by it’s creator Stefan Goessner as:
- Data may be interactively found and extracted out of JSON structures on the client without special scripting.
- JSON data requested by the client can be reduced to the relevant parts on the server, such minimizing the bandwidth usage of the server response.
There are 2 notations we can use while creating JSONPath expressions. One of these is the dot notation that’s closer to the C family of languages:
$.store.book.title, and the other is the bracket notation that likens JSON data to arrays:
$['store']['book']['title'] (Notice the single quotes , they’re very important because some old and widespread versions of JSONPath don’t work with double quotes!). The dollar sign at the beginning of both expressions signifies the root of the JSON data.
Knowing the difference between these two notations is the simple part, and as we’re about to see, they can be used in a mixed fashion. The real advantages JSONPath give us are the logical operations we can do over the data. We can list these operations as follows.
- Being able to use wildcards (
*) for member names and array indexes.
- The descendant operator which allows us to climb back one object.
- Using Python’s array slicing (
[start:end:step]) to be able to do selections on arrays.
- Being able to access the data of the current object with the
- Being able to do logical filtering using special expressions in parenthesis.
The last two points above are JSONPath’s biggest strengths, so I’ll talk about them a bit more. We can use objects’ values against them using expressions in parenthesis:
$.store.book[(@.length - 1)].title
That JSONPath expression would give us the last element of the
book array. Notice the parenthesis and the
@ operator that represents the array.
Again, while using the parenthesis and
@ operator, we can do logical filtering by putting a question mark at the beginning of the parenthesis.
$.store.book[?(@.price < 10)].title
That expression would return the titles of the books whose price are under 10. If we wanted to do such filtering with the JSON libraries of a language like Java or Python, we’d have to write some really long code for it. If the person reading the expression is also familiar with JSONPath, understanding a JSONPath expression is much easier than following bunch of
for loops and
JSONPath has even more operators and functions then I have mentioned here, with countless examples for all. If you visit this page, which is the original source of JSONPath, you can find a list of all operators, plus plenty of examples. Additionally, there is a very popular Java implementation on Github.