Goodreads thumb

An Introduction to the Goodreads APIs

JSON and XML Parsing

How do we pass this data?


JSON ("JavaScript Object Notation") is a readable text/string adaptation of javaScrip that uses objects consisting of object/attribute and value pairs. While it comes from JavaScript, it can be generated and parsed in many other programming languages. It is widely used with APIs (arguably more than XML) but Goodreads uses JSON, XML and sometimes the option to choose between the formats to get back from your API requests. To get a look at it, compare the following:

Here is the raw data back:


and here it is presented as JSON:

				"books": [{
					"id": 265130,
					"isbn": "0679600841",
					"isbn13": "9780679600848",
					"ratings_count": 173,
					"reviews_count": 340,
					"text_reviews_count": 23,
					"work_ratings_count": 156294,
					"work_reviews_count": 406706,
					"work_text_reviews_count": 6703,
					"average_rating": "4.10"

While both could feasible be parsed, or examined to find the desired/relevant information, by the eye, but the JSON version is much more readable. You can get more information on JSON at:


XML, or "eXtensible Markup Language" looks at lot like HTML to the eye. It contains data demarcated by tags, and was designed to be generally and widely usable and focused on documentation. It is widely used for resenting data structures as those passed via an API like Goodreads, and also in a number of office applications such as MS Office, Open Office, etc.

Goodreads generally uses XML for exchanges of larger quantity of text data, while JSON for smaller strings or collections of text, but some methods offer the option to choose using a parameter.

Here is an example of raw XML data from the Microsoft XML Core Services (MSXML) SDK:


For more information on XML, check out:

Parsing JSON:

Parsing is the process of breaking something, like a string or text, down into logical syntactic components, so it can be more easily, or partially, used. In your JavaScript, the JSON and XML is parsed differently. As you might expect, JavaScript can easily handle JSON, with a built in parsing function:

			//where "text" is valid JSON text
			var someObj = JSON.parse(text);

You can also use JavaScript to easily convert into JSON, with:

			//the reverse, turning a JS object to a JSON text
			var text = JSON.stringify(someObj);

We can combine this with the GET syntax we've already been using in JavaScript to create something like:


In this example, we've taken a GET as before, but added several new elements. Instead of pre-defining the ISBN as a variable, we're getting our search term as text entry from the user on our HTML page (far more useful!) under the ID name 'searchIn'. We "stringify" this (though it is already text) as 'toSend' and concatenate it with our GET request URL ('') and the format option (here '.json'), the key ('?key=' + devKey) and our string search term ('&isbn=' + toSend).

Parsing XML:

While XML is already in a tag delimited format, we will often need to traverse these in order to select or display what we are looking for. There is nothing quite so easily built-in as JSON.parse() or JSON.stringify, there are numerous means of traversing XML data in JavaScript and JQuery. (Note there are far easier means using JQuery, but for this tutorial we will continue with JavaScript as our scripting language.)

Let's look at one means of traversing XML data we have gotten back from our API query (we'll look at another later):


As you can see, we're continuing to build on our query, now taking XML search data back. Little is changed (we're getting) a full search back now, like the search engine we will be finishing -- and in order to display the data, we need to parse it to remove the XML. As you can see from the comments, this specific code solution comes from stackoverflow. Click the link below to read the original presentation of this process, which grabs the XML and stores it for extraction:

Check out W3 Schools for Lots of XML Content!

Visit stackoverflow for the source of this XML parsing and more!

Better GETS »