'JSONView' Articles

JSONView is back

On November 14th of 2017, Firefox 57 was released, and JSONView stopped working. This was not unexpected - Mozilla had been warning for a while that they were going to shut off their old extension system completely and move to the WebExtension system pioneered by Google Chrome. All of the Firefox extensions using the old system were silently disabled.

I had hoped to have a new version of JSONView available that was compatible with this new system before Firefox 57 released, but unfortunately the deprecation coincided with a crunch period at work and I wasn’t able to make the time. However, last month I had a chance to relax for a bit while out of town for my partner’s show at Archimedes Gallery in Cannon Beach and I took a couple hours to hack out a WebExtension version of JSONView.

I had always been hesitant to write a WebExtension version (or a Chrome version) because the way JSONView works on Firefox is that it actually intercepts the request and rewrites it into HTML. Existing Chrome ports of JSONView ran on every page load and did stuff to try and rewrite the page, imposing a cost and a security exposure to every page. However, Firefox’s implementation of WebExtensions includes StreamFilter which lets me intercept and rewrite content in the same way as I did before!

Once I had a version working on Firefox, I got something working on Chrome as well. This works differently because StreamFilter is not available on Chrome, so I use a technique that still reformats the page after it loads, but it figures out which pages to act on in a fast, safe way. Thus, for the first time, there is an official JSONView for Google Chrome. If and when Chrome supports StreamFilter, JSONView will start using it transparently, with no changes required.

There are a few bits of functionality that had to be left behind. JSONView had the option to add application/json to your Accept header in order to convince APIs using content negotiation to serve JSON. That turned out not to be a popular way to write APIs, and JSON has since won over XML completely, so this option isn’t as necessary. On the plus side, the method by which I detect content types that contain JSON is much more flexible now, so any content type matching application/*+json will now work with JSONView as well - a long standing feature request.

On Firefox, you may notice that local files open in Firefox’s own JSON viewer instead of JSONView. To fix this, go to about:config and set devtools.jsonview.enabled to false.

JSONView remains a mature project that doesn’t need to change much, but I’m really happy that people can continue using it, and now Chrome users can get the same experience. If you don’t already have it, download JSONView for Firefox or Chrome.

JSONView - View JSON documents in Firefox

I’m a big fan of JSON as a data exchange format. It’s simple, lightweight, easy to produce and easy to consume. However, JSON hasn’t quite caught up to XML in terms of tool support. For example, if you try to visit a URL that produces JSON (using the official “application/json” MIME type), Firefox will prompt you to download the file. If you try the same thing with an XML document, it’ll display a nice formatted result with collapsible sections. I’ve always wanted a Firefox extension that would give JSON the same treatment that comes built-in for XML, and after searching for it for a while I just gave up and wrote my own. The JSONView extension (install) will parse a JSON document and display something prettier, with syntax highlighting, collapsible arrays and objects, and nice readable formatting. In the case that your JSON isn’t really JSON (JSONView is pretty strict) it’ll display an error but still show you the text of your document. If you want to see the original text at any time, it’s still available in “View Source” too.

JSONView logo

I’ve been eager to release this for some time, but I finally pushed it to addons.mozilla.org last night. I actually started development on it about 7 months ago, but work got paused on it for about 6 months due to stuff out of my control, and then I had some other projects I was working on. The actual development only took a few days (including digging through some confusing Unicode bugs). I thought it was funny that right as I was resuming work on JSONView I noticed that a JSON explorer had actually landed for Firebug 1.4, which I’ll also be looking forward to. Initially I had intended to build that functionality as part of my extension. There’s a lot I’d like to add on, like JSONP support and a preference to send the “application/json” MIME type in Firefox’s accept headers.

This is actually my first real open source project - I’ve released some code under open source licenses before, but this is actually set up at Google Code with an issue tracker and public source control and everything. I’ve licensed it under the MIT license. I’m really hoping people get interested in improving the extension with me. I’ve pre-seeded the issue tracker with some known bugs and feature requests.

The extension itself is pretty simple. I wasn’t sure how to approach the problem of supporting a new content type for Firefox, so I followed the example of the wmlbrowser extension and implemented a custom nsIStreamConverter. What this means is that I created a new component that tells Firefox “I know how to translate documents of type application/json into HTML”. And that it does - parsing the JSON using the new native JSON support in Firefox 3 (for speed and security) and then constructing an HTML document that it passes along the chain. This seems to work pretty well, though there are some problems - some parts of Firefox forget the original type of the document and treat it as HTML, so “View Page Info” reports “text/html” instead of “application/json”, “Save as…” saves the generated HTML, Firebug sees the generated HTML, etc. Just recently I came across the nsIURLContentListener interface, which might offer a better way of implementing JSONView, but I’m honestly not sure - the Mozilla documentation is pretty sparse and it was hard enough to get as far as I did. I’m hoping some Mozilla gurus can give me some pointers now that it’s out in the open.

Right now the extension is versioned at “0.1b1” which is a wimpy way of saying “this is a first release and it could use some work”. It’s also trapped in the “sandbox” at addons.mozilla.org, where it will stay until it gets some downloads and reviews. Please check it out, write a little review, and soon people won’t have to log in to install it!

Note: While composing this post I ran across the JSONovich extension which was apparently released in mid-December and seems to do similar stuff to JSONView. No reason we can’t have two competing extensions, though.