Loading JSON


What is JSON Data? JSON stands for Javascript Object Notation. It is a commonly used data interchange format. It is convenient to use in Javascript because it can store exactly the same types of data that Javascript supports. We've alread seen JSON data, but here is an example covering the various types of data it represents:

  graphType: "bar",
  color: "orange",
  numberOfBars: 20,
  percentAwesome: .75,
  xAxisLabels: ["abc", "def", "hij"],
  nestedObject: { "field with spaces": "value"  },
  isObject: true

As expected you can store all of the types of data you are used to storing in Javascript including: strings, integers, decimals, arrays, booleans and nested objects. The format is "key : value".

Typically when JSON is used to represent many pieces of data, it is stored as an array of JSON objects, which looks like a standard Javascript array.

  { letter: "a", count: 10 }, 
  { letter: "b", count: 20 },
  { letter: "z", count: 2 } 

Loading JSON into D3

The d3.json() function is used to load JSON data. Like the d3.csv() function, it takes two parameters, a json file to load and a callback of the form function(error, data). Below is an example of it in use.

// This is the callback function
function draw(error, data) {
  if (error !== null) 
    // it's always a good idea to handle the case where you're data doesn't load!
    alert("error loading data: " + error);
  // create the table
  var tbl = d3.select("#example2").append("table");

  // create the table headers
    .data(["Position Title", "Start Date"])
    .text(function (d) { return d; });

  // Use neseting to create a tr for each record and a td for position title and start date.
  tbl.selectAll(".data tr")
    .classed("data", true)
        // I create a new data array from the existing data
        // This new data is the columns I want to create for each tr
        return [d.position_title, d.start_date];
    .text(function (d) { return d; });

d3.json("govt_software_jobs.json", draw);

For reference one record of "govt_software_jobs.json" is below. I've formatted it to make it easy to read.

  "position_title":"Software Developer/Engineer",
  "organization_name":"U. S. Sentencing Commission",
  "locations":["Washington, DC"],

The United States government has a Job Search API that returns JSON data. I have cached the results of a search for "software" jobs and displayed some of the data in the table above. The terms of service for their API require that I state "USA.gov and the federal government cannot vouch for the data or analyses derived from these data after the data have been retrieved from USA.gov." The search I used was http://api.usa.gov/jobs/search.json?query=software and I retrieve the data on January 5th, 2015. The results are stored in the file "govt_software_jobs.json".


The above is valid JSON. True/False.

In the callback passed to d3.json, the first parameter is the data 
and the second is the error object. True/False

Things to do

We're going to use "govt_software_jobs.json" to create a list of links to USA Government job detail pages.

  1. Create a callback function draw(error, data). It should append a "p" element for each data point passed in. Set the text to be that of the data point. Test it by calling draw([], [1,2,3]) after defining it.
  2. Call draw using d3.json() using "govt_software_jobs.json" instead of the test harness.
  3. Change draw so that each "p" element contains an "a" element. Set the "href" attribute to the posting's URL. Set the text to be the minimum amount offered and the position title. Use the money function to format the minumum amount to look pretty.

Extra Credit

Change the code on the left. Once you've made a change, the page will render on the right.