Weekly Research: JSON Pointer

While looking into JSON Schema last week I stumbled upon JSON Pointer which is an IETF RFC by Paul C. Bryan, Kris Zyp and Mark Nottingham and is used to reference a specific value within a JSON object. Don’t think of it as something like XPath or XQuery, though, since they effectively allow you to select multiple elements that match certain criteria. For this but in JSON there are other tools like JSON Path and JSONiq. This here is more like the id-references in HTML documents using the hash-name but using the nested structure provided by the object instead or artificial IDs.

Let’s say you have following JSON object:

    “title”: “JSON Pointer”,
    “authors”: [
        {"name": "P. Bryan"},
        {"name": "K. Zyp"},
        {"name": "M. Nottingham"}

You’d use following JSON Pointer to reference the name of the first author: /authors/0/name. If all you want is the whole authors array, just use /authors. Don’t append a “/” to the pointer if you want the whole array or sub-object. So, object properties are simply accessed by their name without quotes and array positions by their index… that’s basically it. As I said, this is just for addressing values and since JSON is rather simple, referencing a specific value isn’t all that hard either.

There are two special characters that have to be escaped, though, according to the specification if used within a path token: “/” and “~”. The slash is escaped with “~1” while the tilde is escaped with “~0”. The main reason for this (compared to going with something like URI-encoding) seems to have been performance-considerations, but it also helps prevent some confusion if you use pointers as URI fragments since there you’d have to use URI encoding anyway.


So far I found three implements:

Sadly, I didn’t have the time (or the use-case) to play with any of them yet :-)

What’s coming next?

As much as I’d like to get into JSONiq and other JSON processing and querying languages right now, I think it’s time for something else. So, next week I will either take a break or write about something that is not another RFC about how to deal with JSON. I promise ;-)

comments powered by Disqus