CSV and JSON are two of the most common kinds of files for open data you’ll find on the web today. CSVs are just spreadsheets and are a common way to let people download data in bulk. Tables of data are simple, and a great many people are used to working with them in Excel or Google Docs.
JSON has become the format of choice for APIs on the web today, but JSON data is trickier to work with for many people. Partly, this is because tables are just simpler for people to understand, and JSON data is rarely organized like a table. But maybe more importantly, there are great visual **tools** for working with CSVs, and few such tools for JSON.
In other words, if you send someone a link to a CSV, their browser downloads a file for them and they open it in Excel. If you send someone a link to JSON, their browser displays them a bunch of gobbledygook.
No good: This gives JSON a bad name.
### Making JSON more approachable
A few weeks ago, I gave a workshop at [Open Data Day here in D.C.](http://sunlightfoundation.com/blog/2014/03/03/open-data-day-2014-dcs-biggest-ever/) the goal of which was to demystify JSON and make it feel as approachable as a spreadsheet.
I searched, but couldn’t find any good tools to do this that worked inside the browser. The only solution was to make a new one!
So now, if you go to [konklone.io/json](http://konklone.io/json/), you’ll see a box to paste JSON into:
It’ll then quickly reformat and re-color your JSON…
…and transform it into a table of data below, showing you the first few rows.
You can then click a link above the excerpt to download the full table as a CSV. At any time, you can click “Create a permalink” to save the data and change the URL to a unique link. Anyone visiting this permalink will see that same highlighted JSON and table.
For example, [konklone.io/json/?id=9453362](http://konklone.io/json/?id=9453362) shows recent new US laws from our [Congress API](http://sunlightlabs.github.io/congress/), and [konklone.io/json/?id=9478214](http://konklone.io/json/?id=9478214) shows the [official data catalog of the Treasury and IRS](http://www.treasury.gov/data.json).
And that’s all you need to know to use it. If you’re interested in the technical underpinnings, read on.
### How it works
The whole thing is mostly just other people’s work glued together.
When you paste in JSON, the converter parses it and then guesses at what the “rows” are by [rifling through the JSON](https://github.com/konklone/json/blob/gh-pages/assets/site.js#L44-L59) for an array. Each row is [recursively flattened](https://github.com/konklone/json/blob/gh-pages/assets/site.js#L13-L40) using an adapted algorithm from [csvkit](http://csvkit.readthedocs.org/en/latest/scripts/in2csv.html), a terrific command line tool by [Chris Grozkopf](https://twitter.com/onyxfish) et al for messing with CSV (and the inspiration for this project).
Flattened rows are fed into [jquery-csv](https://code.google.com/p/jquery-csv/), at Ben Balter’s [suggestion](https://twitter.com/BenBalter/status/436703435631915008), and that produces the raw CSV string. The converter then makes a `data:` URI out of the CSV, and updates the download link. The download link takes advantage of a [relatively recent feature](http://updates.html5rocks.com/2011/08/Downloading-resources-in-HTML5-a-download) in HTML5, the `download` attribute for `` tags, to tell the browser to download the linked content rather than display it.
All of this makes it possible to run the whole converter **without a server**, inside the browser, as a static site.
### Github as infrastructure
Github’s public services make this easy.
The website is hosted for free, via [Github Pages](http://pages.github.com/). To make permalinks work, the converter stores raw JSON in an [anonymous Gist](http://konklone.io/json/?id=9478214) using the [Github API](http://developer.github.com/v3/gists/). (There’s a rate limit of 60 anonymous API requests per-hour per-visitor, but in practice that’s been fine.)
And of course, Github is where you can [find the code](https://github.com/konklone/json), which is [dedicated to the public domain](https://github.com/konklone/json/blob/gh-pages/LICENSE).
It’s not perfect — it can’t handle every possible JSON structure you can throw at it, it could be better at telling you about errors and there are surely other bugs. Please describe any issues or suggestions on the issue tracker, which is once again [over on Github](https://github.com/konklone/json/issues).
### It’s not so bad
If you want to get more comfortable with JSON and APIs, do as the developers do and make it easier on your eyes by installing a browser extension:
* For Chrome, [JSON Formatter](https://chrome.google.com/webstore/detail/json-formatter/bcjindcccaagfpapjjmafapmmgkkhgoa?hl=en) * For Firefox, [JSONView](https://addons.mozilla.org/en-us/firefox/addon/jsonview/)
They both do a terrific job of automatically reformatting and highlighting JSON so you can understand what’s going on, and both of them let you easily copy/paste JSON. Give it a shot on the JSON pictured at the top of this page, at the Sunlight Foundation’s description on Facebook’s Graph API ([graph.facebook.com/sunlight-foundation](http://graph.facebook.com/sunlight-foundation)).
Either way, the big thing I tried to communicate at Open Data Day this year is that using JSON (and URLs, APIs and most things on the web!) doesn’t require a computer science degree. It’s all just patterns, meant for both humans and computers to understand.
So the next time someone tells you about an API that uses JSON, go ahead and open it in your browser and look it over. Maybe read through the API’s documentation and try to figure out how to get the JSON you want. You’ve always got a [CSV converter](http://konklone.io/json/) around if you want to download it as a spreadsheet. You’ve got nothing to lose.